Saturday, March 13, 2010

ClientIDs in databound ASP.NET 4 controls

I am working on a project where I have recently changed the clientIDMode to "Static" in the web.config file. This seemed great because all of the output would have clean ids, however it broke asp.net ajax control toolkit controls that were nested in databound controls (such as a gridview).

There is an easy fix for the scenario.

What I did was set the ClientIDMode to "Predictable" on the gridview. I also set ClientIDRowSuffix to the name of the Primary Key field of my datasource. What is nice about using "Predictable" is that the ClientID values do not contain strings like "ctlxxx". Here is a quick example:

<asp:GridView ID="gvGroups" runat="server" AllowPaging="True" SkinID="GridView"
AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="GroupID" ClientIDMode="Predictable" ClientIDRowSuffix="GroupID"
DataSourceID="SqlDataSourceGroups" OnRowCommand="gvGroups_RowCommand">

0 comments: