65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Jun 22, 2012

CPOL

1 min read

viewsIcon

28359

downloadIcon

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.

    Figure 1. Add class Liabrary Project.
  2. Add Javascript File and in properties window set properties as figure 2.
  3. 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
  4. [assembly: System.Web.UI.WebResource("AddJsFunction.JSDate.js", "text/javascript", PerformSubstitution = true)]
  5. Now Write this code for including JavaScript webresource file in calling aspx page
  6. 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);
            }
  7. Now write this code for assign attributes to controls
  8. 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 + ");");
                }
            }
  9. 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

<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

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.

Points of Interest

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