ASP.Net Script Tag

The programmers used to hard code the id of the control in the java script. That will make lot of issues. Because those id prefix will change based on the parent control.

 

Consider an example like, ctl100_pnlCustomers_txtCustName hardcoded in the JavaScript to fetch the value of the textbox.  Later on it needs the design change and someone added another parent control to the text box then it will add another prefix. That has to be changed throughout the page.

 

But when ASP.Net supports to get the id of the control then why can't we can do it?

 

<asp:panel  id=”pnlCustomers” runat=”server”>

<table>

<tr>

<td>Customer Name:</td>

<td><asp:TextBox  id=”txtCustName” runat=”server”></asp:TextBox></td>

</tr>

</table>

</asp:Panel>

 

Instead of hard code something likes this document.getElementById (‘ctl100_pnlCustomers_txtCustName').value can be done in better way using the script tag.

document.getElementBy(‘<%= txtCustName.ClientID  %>').value.

This will get the actual id of the control as per the rendered HTML