Geeks With Blogs
Rajesh Pillai My experiments with asp.net mvc, jquery, wpf, silverlight, sharepoint, tdd and design patterns.

ASP.NET 4 Setting Client IDs

Another most wanted feature of asp.net 4 is the ability control client ID generation.  At least I am happy with this.  With prior
version of asp.net it was always difficult control the control id generation though not impossible.  Why is this important?  If you
can control ID generation then the markup size may dramatially reduce which may result in improved performance.  The gain
may around 15% reduction in terms of markup size in case of complicated nested pages. 

The new ClientIDMode property addresses this long standing issue in ASP.NET.  Knowing the id attribute for rendered
elements are important if your application uses client script that references these elements.

The new CLientIDMode property lets you specify more precisely how the client ID is generated for controls.  You can set the
ClientIDMode property for any control, including for the page.  Following are the possible settings

  • AutoID 
    This is the same as earlier versions of asp.net.
  • Static 
    This specifies that the ClientID value will be the same as the ID without concatenating the IDs of parent naming
    containers.  This can be useful in Web user controls. 
  • Predictable
    This option is primarily for use in data controls that use repeating templates.  It concatenates the ID properties
    of the control's naming containers, but generated ClientID values do not contain strings like "ctlxxx".  This setting
    works in conjuction with the ClientIDRowSuffix propertyof the control.  You set the ClientIDRowSuffix property to
    the name of a data field, and the vlaue of that field is used as the suffix for the generated ClientID value.  It's
    recommended to use the primary key of a data record as the ClientIDRowSuffix value.
  • Inherit
    This setting is the default behaviour for controls.  It specifie that a control's ID generation is the same as its parent.

ClientIDMode can be set at the page or control level or in the configuration.  To set it at the page level refer the below example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default"
                      ClientIDMode = "Predictbale" %>

The following example shows hwo to To set it in the configuration file
<system.web>
       <pages clientIDMode ="Predictable"></pages>
</system.web>

For detailed reading refer
www.asp.net/LEARN/whitepapers/aspnet4/default.aspx

I have found this feature very handy.  Hope you will like it too.  

Happy Programming !!!

Posted on Saturday, November 28, 2009 3:46 PM ASP.NET 4.0 , .NET 4 | Back to top


Comments on this post: Tip : ASP.NET 4 Setting Client IDs

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Rajesh Pillai | Powered by: GeeksWithBlogs.net