Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Javascript

How to Add and Use Javascript Files's Functions in DLL

5.00/5 (3 votes)
22 Jun 2012CPOL1 min read 28.3K   401  
How to add and use JavaScript files's functions in DLL

In this section you learn how to take advantage of JavaScript function in DLL. The basic features of the DLL is to apply JavaScript functions to aspx controls. In particular, you learn how to add JavaScript file and use js functions in aspx page controls.

How to Add Javascript File in DLL

  1. First create new project and select class Liabrary project.

    Image 1

    Figure 1. Add class Liabrary Project.
  2. Add Javascript File and in properties window set properties as figure 2.
  3. Image 2

  4. Now in the AssemblyInfo.cs file add following lines it is very important line so add excatly same name of js file. below is the code
  5. C#
    [assembly: System.Web.UI.WebResource("AddJsFunction.JSDate.js", "text/javascript", PerformSubstitution = true)]
  6. Now Write this code for including JavaScript webresource file in calling aspx page
  7. C#
    public ClsCommonJsFun(Page pg)
            {
                IncludeJsFile(pg);
            }
    
     public void IncludeJsFile(Page  pg)
            {
                pg.ClientScript.RegisterClientScriptInclude(this.GetType(), "Test", pg.ClientScript.GetWebResourceUrl(this.GetType(), "HtmlReporter.JSDate.js"));
                string csslink = "<script type='text/javascript' language="'javascript'" src='" + pg.ClientScript.GetWebResourceUrl(this.GetType(), "HtmlReporter.JSDate.js") + "' />";
                LiteralControl include = new LiteralControl(csslink);
                pg.Header.Controls.Add(include);
            }
  8. Now write this code for assign attributes to controls
  9. C#
    public void SetNumberTextBox(params TextBox[] txt)
            {
                for (int i = 0; i < txt.Length; i++)
                {
                    txt[i].Attributes.Add("onkeypress", "return check_Number_Enter(" + txt[i].ClientID + ");");
                }
            }
  10. First build the DLL then add the New Website and add refrence of this DLL. In your aspx page add texboxes add call the function of the DLL SetNumberTextBox you see that after this you can only enter numeric data only. Actually we are using webresource file and its js function.

.aspx Page Design

XML
<form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

        <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>

    </div>
    </form>

Now the .cs page code for setting texbox to attributes of js function

C#
ClsCommonJsFun cljs = new ClsCommonJsFun(this);
        cljs.SetDateMask(TextBox1,TextBox2);
        cljs.SetNumberTextBox(TextBox3, TextBox4);

See this figure of solution explorer in which I display the DLL project and a website page for testing that js functions is assigned to aspx controls or not.

Image 3

Points of Interest

I work in ASP.NET with C#. I want to learn JavaScript to work more efficiently like a desktop application.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)