C# telerik grid cell value from template column

alinnemet picture alinnemet · May 6, 2011 · Viewed 11.6k times · Source

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" Text='<%# 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.

Answer

alinnemet picture alinnemet · May 9, 2011

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;
            }