C# telerik grid cell value from template column
I have a grid-template-column defined like this( to save time and space i'll only put the column):
<telerik:GridTemplateColumn HeaderText="Id" Reorderable="true" SortExpression="Id" UniqueName="Id" DataField="Id">
<ItemTemplate>
<asp:Label ID="lblId" runat="server" T开发者_如何学JAVAext='<%# Eval("Id") %>' />
</ItemTemplate>
<EditItemTemplate>
<telerik:RadTextBox ID="txbId" Width="50px" runat="server" TextMode="SingleLine"
Text='<%# Bind("Id") %>' />
</EditItemTemplate>
<ItemStyle VerticalAlign="Top" />
</telerik:GridTemplateColumn>
And I want on the PreRender event, to extract the value of this column
protected void RadGrid1_PreRender(object sender, System.EventArgs e)
{
//string selectedItem = ((GridDataItem)RadGrid1.SelectedItems[0])["Id"].Text;
foreach (GridDataItem item in RadGrid1.Items)
{
//not working
string k = item["Id"].Text;// is empty string
string key = (item["Id"].TemplateControl.FindControl("lblId") as RadTextBox).Text;// null pointer
}
Any idea how to fix it?
Thx a lot.
It seems that the solution is rather simple, responded on telerik forum:
foreach (GridDataItem item in grdHeader.EditItems)
{
// if in editing mode
GridEditableItem edititem = (GridEditableItem)item.EditFormItem;
RadTextBox txtHeaderName = (RadTextBox)edititem.FindControl("txbId");
//otherwise
Label lbl= (Label)edititem.FindControl("lblId");
string id = lbl.Text;
}
I could be mistaken (as I'm not super familiar with the Telerik control suite), but normally, data binding events don't occur until after the control's PreRender event. You'll have to DataBind earlier, or move your logic to later in the page life cycle.
Try
foreach (GridDataItem item in RadGrid1.Items)
{
if(item.ItemType == GridItemType.Item ||
item.ItemType == GridItemType.AlternatingItem)
{
string k = item["Id"].Text;// is empty string
...
精彩评论