This is a migrated thread and some comments may be shown as answers.

RadGrid ItemTemplate GridDataItem .TEXT is empty

1 Answer 274 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Brian Meagher
Top achievements
Rank 1
Brian Meagher asked on 21 May 2010, 06:34 PM
Hi,

I am attempting to retrieve the value of a cell in a RadGrid during the ItemDataBound event. The catch is that I am using an ItemTemplate, which seems to be the root of the problem.

I found http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html which states (about half-way down) that “This approach of obtaining cell values works for auto-generated columns and built-in column types except for template columns. For template columns you must find the control in the grid cell and extract its value.”

I agree with that statement (based on my testing), but I cannot find anywhere an example of “finding the control in the grid cell and extracting its value.”

Can someone point me to a relevant example?

My goal is to read the URL for a picture (image source), and set an imagecolumn on the same row to use that image source. This approach works perfectly so long as I do not use an <ItemTemplate></ItemTemplate> block.

Thank you in advance for your help.

Brian Meagher

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 24 May 2010, 11:48 AM
Hello Brian Meagher,

You need to access the GridDataItem and get reference to control using the FindControl(controlId) method. Here is an example.

ASPX:
 
   <telerik:GridTemplateColumn DataField="Username" HeaderText="Username" UniqueName="Username" 
        SortExpression="Username"
        <ItemTemplate> 
            <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Users.aspx">HyperLink</asp:HyperLink> 
        </ItemTemplate> 
    </telerik:GridTemplateColumn> 

C#:
 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridDataItem)) 
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            HyperLink hLink = (HyperLink)dataItem.FindControl("HyperLink1"); // Access the control 
            String url = hLink.NavigateUrl;  // Get the url        
        }  
    } 


-Shinu.
Tags
Grid
Asked by
Brian Meagher
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or