Click here to Skip to main content
12,998,759 members (68,461 online)
Rate this:
Please Sign up or sign in to vote.
See more:
how to call control define in web page through separate javascript file
<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()
Posted 15-Nov-12 22:30pm
Tanveer A1.3K
Updated 16-Nov-12 6:07am
Ravi Tuvar 16-Nov-12 6:12am
are you using jquery or just simple javascript in your page?
Mathlab 16-Nov-12 8:49am
You look like you have the call right but the script tag is in the wrong position
Tanveer A 17-Nov-12 2:11am
just using simple javascript
plz tell me how to solve this by jquery

1 solution

Rate this: bad
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) {

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.
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
Web02 | 2.8.170622.1 | Last Updated 16 Nov 2012
Copyright © CodeProject, 1999-2017
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