Click here to Skip to main content
Click here to Skip to main content

Tagged as

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

, 22 Jun 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

Prosan
Software Developer (Senior)
India India
i work in ASP.Net,C#,Sql,VB6.0 etc. I want to enhance my skills by learning new technologies.

Comments and Discussions

 
GeneralMy vote of 5 PinmemberProsan22-Jun-12 23:50 
GeneralMy vote of 5 PingroupTechnoses22-Jun-12 21:31 
SuggestionMy Article on This Topic PinprotectorAspDotNetDev22-Jun-12 13:47 
GeneralRe: My Article on This Topic PinmemberProsan22-Jun-12 23:48 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 22 Jun 2012
Article Copyright 2012 by Prosan
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid