Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / Javascript
Article

Referencing server controls from Javascript

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
11 Oct 2013CPOL1 min read 5.7K   1  
 In this article we will show you how to reference server side controls from JavaScript code (A server control is a control that has a

This articles was originally at wiki.asp.net but has now been given a new home on CodeProject. Editing rights for this article has been set at Bronze or above, so please go in and edit and update this article to keep it fresh and relevant.

 

In this article we will show you how to reference server side controls from JavaScript code (A server control is a control that has a runat="server" attribute).

Generally, every server control has an ID on the server side. One common problem when accessing the server side controls from JavaScript is that developers assume that the control's server side ID can be used directly in JavaScript code to reference the rendered control on the client side.  This is incorrect assumption.

The client side ID of the rendered control is not always the same as the ID on the server side. If you have placed the server side control inside another control which implements the INamingContainer marker Interface, the runtime will change the controls client side ID to enforce the uniqueness of that ID in the rendered html document. You can get the resulting client side ID through the controls ClientID property.

Note: You will notice this behavior when you start working with MasterPages or when you place the control inside GridView control or any other control that implements INamingContainer Marker Interface.

The following example will show you how to reference the Input control using its ClientID property:

<script language="javascript" type="text/javascript"><br />function incrementValue() {<br />    var txtToIncr = document.getElementById('<%= txtToIncr.ClientID %>');<br />    var CurrentValue = 0;<br />    if (txtToIncr.value != '')<br />        CurrentValue = parseInt(txtToIncr.value);<br />    txtToIncr.value = CurrentValue + 1;<br />}<br /></script>

<div><br />    <input id="txtToIncr" type="text" runat="server" value="0" /><br />    <asp:Button ID="BtnIncrement" runat="server" Text="Increment From Client Side" UseSubmitBehavior="false"<br />        OnClientClick=" return incrementValue();" /><br />   <asp:Button ID="BtnIncrementFromServer" runat="server" Text="Increment From Server Side" /><br /></div>

Code Behind:

Protected Sub BtnIncrementServer_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnIncrementServer.Click<br />        Dim Currentvalue As Integer = 0<br />        If (txtToIncr.Value <> "") Then<br />            Currentvalue = Integer.Parse(txtToIncr.Value)<br />        End If<br />        txtToIncr.Value = Currentvalue + 1<br /> End Sub

In the example, there are two TextBox controls.  The first one "BtnIncrement" can be used to change the Input control value via JavaScript code and the other control "BtnIncrementFromServer" is used to change the Input value from server side code.

 

License

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


Written By
United States United States
The ASP.NET Wiki was started by Scott Hanselman in February of 2008. The idea is that folks spend a lot of time trolling the blogs, googlinglive-searching for answers to common "How To" questions. There's piles of fantastic community-created and MSFT-created content out there, but if it's not found by a search engine and the right combination of keywords, it's often lost.

The ASP.NET Wiki articles moved to CodeProject in October 2013 and will live on, loved, protected and updated by the community.
This is a Collaborative Group

754 members

Comments and Discussions

 
-- There are no messages in this forum --