Friday, 23 November 2012

ASP.NET Textbox Tips & Tricks

This page is to describe all tips and tricks of asp.net controls. We have number of asp.net server controls which we use regularly and may miss few things and will search a lot for the tip or trick to handle the situation such situation we will discuss regularly.

This page discusses about the basic asp.net control textbox

The syntax for the textbox is as below

Syntax:

<asp:TextBox runat="Server" ID="txtName"></asp:TextBox >

It is a input field means from which users can input data and will be used by our application to process it.



Rendering: 

It renders as normal HTML input element of type text

TIP#1- Accessing Textbox from Javascript:

    As it renders as HTML element to the user browser any HTML element can be accessed by Javascript so we can access this control too. Only what we need is to know the element id after rendering. For that asp.net provided us a very good property for dealing with client ids. i.e. <control>.ClientID which will return us the rendered element id.

Example:

  <asp:TextBox runat="server" ID="txtName"></asp:TextBox>
  <script type="text/Javascript">
      var elm = document.getElementByID('<%=txtName.ClientID%>');
  </script>

from the above script we can perform all kinds of operation that we can do for a normal input field from Javascript like accessing or assigning a value to textbox, enable or disable textbox, changing the style of textbox, validating the user input in the textbox