Click here to Skip to main content
Sign Up to vote bad
good
See more: JavascriptVB.NET
how to call control define in web page through separate javascript file
like
<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
function abc()
{
   alert(document.getElementByID('<%=txt.ClientID%>').value);
}
Posted 15-Nov-12 22:30pm
Tanveer A1.2K
Edited 16-Nov-12 6:07am

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
Mathlab - 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
 
function abc(txtID) {
    alert(document.getElementById(txtID).value);
}
 
HTML page contains,
 
        <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
 
    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.
  Permalink  
Comments
Tanveer A - 27-Feb-13 0:46am
thankx mr. haris
Sheikh Muhammad Haris - 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)

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Christian Graus 453
1 Ron Beyer 316
2 Tadit Dash 243
3 OriginalGriff 236
4 samadhan_kshirsagar 173
0 Sergey Alexandrovich Kryukov 7,061
1 Prasad_Kulkarni 3,830
2 OriginalGriff 3,620
3 _Amy 3,370
4 CPallini 3,074


Advertise | Privacy | Mobile
Web03 | 2.6.130619.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid