Reputation: 301
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.
Upvotes: 4
Views: 11778
Reputation: 301
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;
}
Upvotes: 2
Reputation: 108957
Try
foreach (GridDataItem item in RadGrid1.Items)
{
if(item.ItemType == GridItemType.Item ||
item.ItemType == GridItemType.AlternatingItem)
{
string k = item["Id"].Text;// is empty string
...
Upvotes: 0
Reputation: 1062
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.
Upvotes: 0