Job Openings @Ajaha

DotNet Job Openings @Ajaha

Friday, 23 November 2012

ASP.NET Textbox Tips & Tricks

This page is to describe all tips and tricks of controls. We have number of 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 control textbox

The syntax for the textbox is as below


<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.


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 provided us a very good property for dealing with client ids. i.e. <control>.ClientID which will return us the rendered element id.


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

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