Controls with the same name in GridView Templates

I've created the GridView with templates like the following:
       <asp:TemplateField SortExpression="Date1">

            <AlternatingItemTemplate>

                <asp:CheckBox ID="chkSelected" runat="server" />

            </AlternatingItemTemplate>

        </asp:TemplateField>

        <asp:TemplateField SortExpression="Date2">

            <AlternatingItemTemplate>

                <asp:CheckBox ID="chkSelected" runat="server" />

            </AlternatingItemTemplate>

        </asp:TemplateField>

On runtime it cause the error:
Message : Multiple controls with the same ID 'chkSelected' were found. FindControl requires that controls have unique IDs.
Type : System.Web.HttpException, System.Web, Version=
2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
Message : Multiple controls with the same ID 'chkSelected' were found. FindControl requires that controls have unique IDs.
Source : System.Web
ErrorCode : -2147467259
Stack Trace : at System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls)
at System.Web.UI.Control.FillNamedControlsTable(Control namingContainer, ControlCollection controls)
at System.Web.UI.Control.EnsureNamedControlsTable()
at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
at System.Web.UI.Control.FindControl(String id, Int32 pathOffset)
at System.Web.UI.Page.FindControl(String id)
at System.Web.UI.Page.ProcessPostData(NameValueCollection postData, Boolean fBeforeLoad)
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

The problem explained on forum "GridViewRow is the naming container in GridView, not individual TableCell.
 
I had to use Dynamic Template columns based on  page http://www.webswapp.com/codesamples/viewsource.aspx?file=~/codesamples/aspnet20/itemplate/checkboxtemplate.cs
Note that in that example the CheckBoxTemplate class declared as implemented INamingContainer.
It causes expectation, that generated cell is naming container, which is not correct.
Because template is not derived from Control, specifying INamingContainer interface has no effect and should be removed.
 
Similar templates are described in

posted @ Monday, May 05, 2008 11:21 PM

Print

Comments on this entry:

No comments posted yet.

Your comment:



 (will not be displayed)


 
 
 
 
 

Live Comment Preview:

 
«November»
SunMonTueWedThuFriSat
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345