Sunday, May 23, 2010

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

Code Snippet
  1. public partial class WebUserControl1 : System.Web.UI.UserControl, INamingContainer
  2. {
  3. protected void Page_Load(object sender, EventArgs e)
  4. {
  5. this.ClientIDMode = ClientIDMode.Static;
  6. }
  7. }

That's all you need to do, and the control gets rendered to the page with an ID of Label1.

C# fast-food by:
Eng.Waleed abou-zaid
Senior Software Developer
Harf Information Technology
MCSD.Net,MCAD.Net,MCP,A+,N+ certified
walid.abouzaid@gmail.com



0 comments: