Access Control Id on Client Side with or Without using Master Page

Here is the method through which you can access Asp.net control's Id using javascript.

When you are working on a simple form.

Let's suppose you want to make a textbox clear or blank on a button click on client side

HTML

  1. <asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>   
Put below javascript code in the <head></head> section of the form:
  1. <script type="text/javascript">    
  2.      function clrCtrl() {    
  3.         document.getElementById('txtPassword').value = "";    
  4.           }    
  5.    </script>    
On button's OnClientClick event we can use clrCtrl() function.

When you are using Master Page

When you are using Master Page, you just need to change the javascript code like below and out it in
Content Section:
  1. <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">    
  2.  <script type="text/javascript">    
  3.     function clrCtrl() {    
  4.    document.getElementById('<%=txtPassword.ClientID%>').value = "";    
  5.     }    
  6.    </script>    
  7.  // here you html code begins    
  8.  </asp:Content>    

 

Ebook Download
View all
Learn
View all