Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to call control define in web page through separate javascript file
like
HTML
<asp:TextBox id="txt" runat="server" />
<asp:Button id="btn" runat="server" onClientClick="abc()"/>
<script type='text/javascript' src='abc.js'/>

javascript file abc.js
JavaScript
function abc()
{
   alert(document.getElementByID('<%=txt.ClientID%>').value);
}
Posted
Updated 16-Nov-12 6:07am
v4
Comments
Ravi Tuvar 16-Nov-12 6:12am    
are you using jquery or just simple javascript in your page?
Tanveer A 17-Nov-12 2:11am    
just using simple javascript
plz tell me how to solve this by jquery
[no name] 16-Nov-12 8:49am    
You look like you have the call right but the script tag is in the wrong position

1 solution

You need to pass the ID of the control as a parameter of your javascript function.

For reference, see the code below.

Your Javascript file abc.js contains following

JavaScript
function abc(txtID) {
    alert(document.getElementById(txtID).value);
}


HTML page contains,

ASP.NET
<script src="abc.js" type="text/javascript"></script>
<asp:textbox id="txtInput" runat="server" ></asp:textbox>
<asp:button id="btn2" runat="server" text="Click Me" />


Then on your server side page load event

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        btn2.Attributes.Add("onclick", "abc('" + txtInput.ClientID + "')");
    }
}


That's it, You are done.



Do Mark as Answer and Vote, if this solution works for you.
 
Share this answer
 
v2
Comments
Tanveer A 27-Feb-13 0:46am    
thankx mr. haris
[no name] 27-Feb-13 3:41am    
Always welcome

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900