asp.net GridView Row's id does not change controls clientid when in Predictable ClientIDMode
I have a gridview being rendered in the format of:
<div>
<table>
<thead>
<tr><th&开发者_JAVA技巧gt;</th></tr>
</thead>
<tbody>
<tr><td></td></tr>
</tbody>
</table>
</div>
Our subclass of the gridview/row etc adds an id to each row
row.ID = "item" + row.DataItemIndex.ToString();
row.ClientIDMode = Predictable;
But when it gets rendered to the page the ClientID
of the controls in the row don't have this id in them.
<TABLE>
<TBODY>
<TR id="MainContent_mygridview">
<TD>
<SPAN id="MainContent_mygridview_label1_0">This is on page one</SPAN>
</TD>
</TR>
<TR id="MainContent_mygridview">
<TD>
<SPAN id="MainContent_mygridview_label1_1">This is on page one</SPAN>
</TD>
</TR>
</TBODY>
<TBODY>
<TR id="MainContent_mygridview">
<TD>
<SPAN id="MainContent_mygridview_label1_0">This is on page two</SPAN>
</TD>
</TR>
<TR id="MainContent_mygridview">
<TD>
<SPAN id="MainContent_mygridview_label1_1">This is on page two</SPAN>
</TD>
</TR>
</TBODY>
</TABLE>
This becomes a problem because I have some ajax on the page that gets the next "page" worth of rows when requested and adds them to the gridview (as an extra tbody
). This causes a clash of ID's in the DOM.
So After digging around I found that the Predictable id uses the DisplayIndex
Property of the GridViewRow
.
int System.Web.UI.IDataItemContainer.DisplayIndex
{
get
{
return this.DataItemIndex;
}
}
Also my subclass:
public class GridViewRow :
System.Web.UI.WebControls.GridViewRow, System.Web.UI.IDataItemContainer
I'm not sure why the subclass needed that as the system gridviewrow already implements that interface so maybe someone else could add to this.
Also I'm not sure if this is going to break anything else.
but now the elements get an id that ends in their DataItemIndex which makes them unique across the dataset instead of just the "page".
精彩评论