Click here to Skip to main content
15,867,906 members
Articles / Programming Languages / Javascript
Tip/Trick

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

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
22 Jun 2012CPOL1 min read 27.3K   399   8   4
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)


Written By
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 Pin
Prosan22-Jun-12 22:50
Prosan22-Jun-12 22:50 
GeneralMy vote of 5 Pin
Technoses22-Jun-12 20:31
Technoses22-Jun-12 20:31 
SuggestionMy Article on This Topic Pin
AspDotNetDev22-Jun-12 12:47
protectorAspDotNetDev22-Jun-12 12:47 
GeneralRe: My Article on This Topic Pin
Prosan22-Jun-12 22:48
Prosan22-Jun-12 22:48 

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

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