Click here to Skip to main content
12,450,981 members (24,104 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB 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 22:30pm
Tanveer A1.3K
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
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

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 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
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160826.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2016
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