Click here to Skip to main content
11,427,997 members (57,464 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript VB.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 23:30pm
Tanveer A1.2K
Edited 16-Nov-12 7:07am
v4
Comments
Ravi Tuvar at 16-Nov-12 6:12am
   
are you using jquery or just simple javascript in your page?
Tanveer A at 17-Nov-12 2:11am
   
just using simple javascript
plz tell me how to solve this by jquery
Mathlab at 16-Nov-12 8:49am
   
You look like you have the call right but the script tag is in the wrong position

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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  
v2
Comments
Tanveer A at 27-Feb-13 0:46am
   
thankx mr. haris
Sheikh Muhammad Haris at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.150428.2 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100