Saturday, March 20, 2010

Get control Id in javascript

We have many times faced the issue of knowing the client id of the control in javascript...well, if you use javascript within an aspx / ascx page you can use the following to get the client ID of the control



alert(document.getElementByID('<%=txtDemo.ClientID %>'));


in fact you can do a lot more than get your control client ID, you can do code with in the <% %> tags. These are called server tags, the code written within these tags is run on the server and the corresponding values are placed. For ex. if you want to get the username in stored in the session,
you can use

var userName = '<%= Session["userName"].ToString() %>';

the <%= %> defines the assignment, tells the server that the values generated should be placed at the same location.

<%# %> this tag is used for binding data in in repeater, datagrid etc...