ASP.NET 4 ClientIDMode Property Explained

There has been some confusion about how to work with this property to your advantage.  With out being to long winded I hope to provide some clarity.

When to change the ClientIDMode of a control?

...if you need to locate specific controls that exist in a repeating data control without capturing events from that control which you are try to reference.  This commonly occurs when you are trying to coordinate user actions to html objects using JavaScript.  

First lets look at the possible values for this property:

AutoID Process used in previous versions ASP.NET  which was prefixing an incrementing value to the control ID
Static Client ID value is set to the value of the ID property
Predictable

Basic Algorithm is either of the following
[parent clientid] + "_" + [ClientIDRowSuffix]
or

[parent clientid] + "_" + incrementing value

Inherit Control inherits the setting of its parent control

For a complete description visit ClientIDMode On MSDN

To demonstrate the affects of each of the property values and how the generated client side ID is affected I will create an web page with several controls:

  1. Using VS 2010 File menu create a new ASP.NET Empty Web Application.
  2. Add a new Web Form
  3. Open the web form
  4. In design view drag and drop two Grid Views onto the design surface making sure they are contained in the default div tag that was provided by the designer.
  5. For both GridViews set the AutoGenerateColumns="false"
  6. We now have two unexciting GridViews that need some data.  Add a new class to the solution.  Name Product and give it two properties (feel free to use the following)
       1: public class Product
       2: {
       3:    public string ProductID{get;set;}
       4:    public string ProductName{get;set;}
       5: }
  7. Open the code behind file for the Web page, for me it is named WebForm1.aspx.cs for me
  8. Create a new method that will be used to populate our GridViews with a list of Products.  (see following code).
       1: protected void Page_Load(object sender, EventArgs e)
       2: {
       3:     LoadProductsToGrids();
       4: }
       5:  
       6: private void LoadProductsToGrids()
       7: {
       8:     List<Product> productList = new List<Product>
       9:     {
      10:         new Product{ProductID="A026",ProductName="Apples"},
      11:         new Product{ProductID="B146",ProductName="Bananas"},
      12:         new Product{ProductID="C276",ProductName="Chiote"},
      13:         new Product{ProductID="D356",ProductName="Drumsticks"},
      14:         new Product{ProductID="E486",ProductName="Elephant Ears"},
      15:         new Product{ProductID="F556",ProductName="Fries"},
      16:         new Product{ProductID="G656",ProductName="Green beans"},
      17:         new Product{ProductID="H756",ProductName="Hay"},
      18:         new Product{ProductID="I856",ProductName="Idaho Potatoes"}
      19:     };
      20:     GridView1.DataSource = productList;
      21:     GridView2.DataSource = productList;
      22:     GridView1.DataBind();
      23:     GridView2.DataBind();
      24: }
  9. To display this data on our web page I will use templated fields because it will easily allow me to demonstrate the affects of the ClientIDMod property.
  10. Open the web page and view the HTML source and add the following templated fields to both of the GridViews
       1: <Columns>
       2:     <asp:TemplateField>
       3:         <HeaderTemplate>Product ID</HeaderTemplate>
       4:         <ItemTemplate ><asp:Label ID="ProductID" runat="server" Text='<%# Bind("ProductID") %>'></asp:Label></ItemTemplate>
       5:     </asp:TemplateField>
       6:     <asp:TemplateField>
       7:         <HeaderTemplate>Product Name</HeaderTemplate>
       8:         <ItemTemplate ><asp:Label ID="ProductName" runat="server" Text='<%# Bind("ProductName") %>'></asp:Label></ItemTemplate>
       9:     </asp:TemplateField>
      10: </Columns>
  11. Now before we run code for two labels contained in the second "GridView2" set the the ClientIDMode to "AutoID"
  12. [Example #1] Running the application you will see two identical grids, but viewing the page source will reveal:
    1. The top grid with the label controls using the default ClientIDMode of Inherit have the control naming pattern ParentContainerID_LabelID_IncrementingValue
    2. The bottom grid with label controls using the default ClientIDMode of AutoID have the control naming patter that was used in prior ASP.NET versions ParentContainerID_ctl+incrementingvalue_IncrementingValue
    3. Inherit_vs_AutoID
  13. [Example#2] Lets change the labels contained in GridView1 to have the ClientIDMode = "Predictable"; Then change the labels in GridView2 to have the ClientIDMode = "Static"
  14. Running the application you will see the following results
    1. The ClientIDMode = "Predictable" behaves as we had seen with the Inherit setting but be careful Inherit will take on its parent setting and could cause some unexpected results when your controls are inside a container such as a Master Page.  So be sure to set your ClientIDMode to predictable if you have dependencies on the control name being ....well...'predictable'
    2. The value of Static, is just like it sounds, this property value causes all iterations of the control to be named the same "ProductID" and "ProductName" respectively.  Regardless of what we do to the containing GridView the labels will continue to repeat the same name.
    3. Predictable_vs_Static
  15. [Example#3] Now we've seen the variations of the ClientIDMode property to see the true value of the property we need to use the ClientIDRowSuffix property that is available to us on the GridView (and some other controls).
    1. On GridView1 set the ClientIDRowSuffix = "ProductID"
    2. On GridView2 set the ClientIDRowSuffix = "ProductID" and ClientIDMode = "Static"
    3. For the labels contained in GridView2 set the ClientIDMode = Predictable
  16. Running the application you will see the following pattern:
    1. For the label controls contained in GridView1 the label IDs have the following pattern: ParentContainerID_LabelID_ProductID
    2. For the controls contained in GridView2 the label IDs have the following pattern: LabelID_ProductID
    3. ClientIDRowSuffix

So you can see that using the ClientIDRowSuffix along with the ClientIDMode you can establish a control ID naming pattern that should support the majority of your needs.

Visual Studio 2010 C# Project Source (ClientIDMode Example)

~Enjoy~


Posted by: Jim Rouse
Posted on: 9/19/2011 at 7:00 AM
Categories: ASP.NET | Web Development
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (0) | Subscribe to this BlogRSS comment feed

Add comment




biuquote
  • Comment
  • Preview
Loading


Contact Us 651-994-8558 1-800-866-9884
Home | Training | Curriculum | Course Finder | Schedule | Enroll | Twin Cities Java User Group | Consulting | Foundation | Jobs | About Us | Our Story | Press Room | Instructors | President | Map & Directions | Sitemap

Java Training | JSF / Struts / Spring / Hibernate Training | Java Power Tools Training | .NET 4.0 & Visual Studio 2010 Training | Microsoft Web Development Training | Prism / MVVM / MEF Training | .NET 3.5 and Visual Studio 2008 Training | .NET 2.0 and Visual Studio 2003 Training | Cloud Computing Training | Ajax / Web Services / XML Training | Groovy and Grails Training | SQL Server 2008 Training | SQL Server 2005 Training | Mobile Development Training | SharePoint 2010 Training | SharePoint 2007 Training | Agile, Process, Analysis & Design Training | Arch/Design Patterns Training | Microsoft Official Curriculum Training | Web Development Training | Ruby Training | Rational Application Developer (RAD) Training | WebSphere Application Server Training | WebSphere Portal Training | WebLogic Training | Boot Camp Training | Project Management Training | C++ Training | Metro / WinRT / Windows 8 Development Training | Retired

Intertech delivers training on-site and virtually serving cities including Phoenix, AZ | San Francisco, CA | Los Angeles, CA | San Diego, CA | San Jose, CA | Washington, DC | Chicago, IL | Orlando, FL | Boston, MA | Duluth, MN | Minneapolis St. Paul, MN | Rochester, MN | Raleigh-Durham, NC | New York, NY | Philadelphia, PA | Austin, TX | Dallas, TX | Houston, TX | Seattle, WA.