Static ClientID ASP.Net 4.0

Static ClientID
When writing JavaScript code to get any control in a page, We don’t know the control client

ID. Now in ASP.Net 4.0 you can make the client id of the control fixed

By making your control implement the following interface:

INamingContainer “ and set the ClientIDMode property to Static.

For example, here is the codebehind for a simple web user control (ASCX) with a label in it.

public partial class WebUserControl1 : System.Web.UI.UserControl, INamingContainer
{
   protected void Page_Load(object sender, EventArgs e)
   {
     this.ClientIDMode = ClientIDMode.Static;
   }
}
That’s all you need to do, and the control gets rendered to the page with an ID of Label1.

Advertisements

Leave a comment

Filed under .Net

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s