You must Sign In to post a response.
  • Category: ASP.NET

    Calling .js file in aspx under button click of a usercontrol

    I have a usercontrol with a textbox and a button. I created 2 instances for that in an aspx page. And i have a sepereate js file for textbox validation. Now i need to call js file that will fire under button click of 2 instances of same button seperately. can anybody please help me how to do that?
  • #762962
    If i understood you correctly then you need to include both js on aspx page and call the js function on onclick event.

    make sense

  • #762963
    TwoInstances.ascx

    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="TwoInstances.ascx.cs" Inherits="TwoInstances" %>


    <asp:TextBox ID="txt_text" runat="server"></asp:TextBox><!--set Autopostback=true for textChanged-->
    <br />
    <br />
           
    <asp:Button ID="btn_submit" runat="server" Text="Submit"
    onclick="btn_submit_Click" />


    TwoInstances.ascx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class TwoInstances : System.Web.UI.UserControl
    {
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_submit_Click(object sender, EventArgs e)
    {

    }

    }



    TwoInstances.aspx

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TwoInstances.aspx.cs" Inherits="TwoInstances" %>

    <%@ Register src="TwoInstances.ascx" tagname="TwoInstances" tagprefix="uc1" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    </head>
    <body>
    <div>
    <form id="form1" runat="server">
    <div style="float:left;width:20%;"/><uc1:TwoInstances ID="TwoInstances1" runat="server" /></div>
    <div style="float:left;width:20%;"/><uc1:TwoInstances ID="TwoInstances2" runat="server" /></div>


    </div>
    </form>
    </body>
    </html>



    TwoInstances.aspx.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    public partial class TwoInstances : System.Web.UI.Page
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    Button btnSubmit1 = (Button)TwoInstances1.FindControl("btn_submit");
    btnSubmit1.Click += new EventHandler(btnSubmit1_Click);
    Button btnSubmit2 = (Button)TwoInstances2.FindControl("btn_submit");
    btnSubmit2.Click += new EventHandler(btnSubmit2_Click);
    }

    void btnSubmit1_Click(object sender, EventArgs e)
    {
    UserControl usercontroltx1 = form1.FindControl("TwoInstances1") as UserControl;
    UserControl usercontroltx2 = form1.FindControl("TwoInstances2") as UserControl;
    (usercontroltx2.FindControl("txt_text") as TextBox).Text += (usercontroltx1.FindControl("txt_text") as TextBox).Text;
    }
    void btnSubmit2_Click(object sender, EventArgs e)
    {
    UserControl usercontroltx1 = form1.FindControl("TwoInstances1") as UserControl;
    UserControl usercontroltx2 = form1.FindControl("TwoInstances2") as UserControl;
    (usercontroltx1.FindControl("txt_text") as TextBox).Text += (usercontroltx2.FindControl("txt_text") as TextBox).Text;
    }
    }




    Thank you for your reply..
    I entered my code above. If i attach the js file in aspx page and call the js function in code behind, it is not working

  • #762964

    you need to register the javascript file in the Page_Load event of the usercontrol as shown below. Here we are assuming that the javascript file is located in the Scripts folder with the name: app.js

    protected void Page_Load(object sender, EventArgs e)
    {
    ScriptManager.RegisterStartupScript(this.Page,typeof(TwoInstances), "MyUniquekey", @"<script src=""Scripts/app.js"" type=""text/javascript""></script>", false);
    }


    Miss. Jain
    Microsoft Certified Technology Specialist in .Net

  • #762969
    Hi sandhya,

    Whenever you want to call clientside scripts in code behind use below lines of code in your desired event.


    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "Title", "YourScript / function", true);


    Hope this will helpful to you..

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/


Sign In to post your comments