4

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.

alinnemet
  • 301
  • 1
  • 3
  • 10
  • Can you do this *during* data binding so that you have access to each item as it's bound? – Yuck May 06 '11 at 15:22
  • I'll try, but would be nice to have it on prerender, because i need to alter the tooltip of some columns based on value from DB, which is only possible on prerender!?. The complication comes when I try to use a template column :/ – alinnemet May 06 '11 at 15:27

3 Answers3

2

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;
            }
alinnemet
  • 301
  • 1
  • 3
  • 10
0

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.

Ian Pugsley
  • 1,062
  • 8
  • 20
  • No, the databind is already done! Because if column were normally binded, like not using template, I can access it using item["Id"].Text – alinnemet May 06 '11 at 15:14
  • What exactly is item["Id"] supposed to be giving you? I only see lblId and txbId, and if you're not editing, txbId shouldn't exist. – Ian Pugsley May 06 '11 at 16:50
  • Sorry for responding so late :)... item["Id"] should give me the unique Id of each item from the grid. Exactly, txbId is bad example there because i'm not editing so i'll edit the post! But there should be a way to access that cell in the OnPreRender event – alinnemet May 06 '11 at 18:10
  • Interesting...not sure why it's not available, as long as that GridDataItem has "Id". Could you loop through the rows themselves and just get the cell, rather than trying to use RadGrid.Items? – Ian Pugsley May 06 '11 at 18:24
  • I wish I know how, 'cause the cell is in a template column :) anyways, I posted this on telerik forum, so they should probably know, but still no answer. If I'll have feedback from them, i'll post it here too. Thx a lot. – alinnemet May 06 '11 at 18:31
0

Try

foreach (GridDataItem item in RadGrid1.Items)
    {
        if(item.ItemType == GridItemType.Item ||
                    item.ItemType ==   GridItemType.AlternatingItem)
        {
            string k = item["Id"].Text;// is empty string 
            ...
Bala R
  • 107,317
  • 23
  • 199
  • 210