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

Cant get a column value in ItemDataBound

3 Answers 617 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mike
Top achievements
Rank 2
Mike asked on 14 May 2010, 02:58 PM
Hi,

I know this is probably a very simple scenario but I cannot work it out. I need to get the "Username" value from a GridTemplateColumn and place it into the ItemDataBound event in the code behind, here is the column:

<telerik:GridTemplateColumn DataField="Username"  
                            HeaderText="Username" 
                            UniqueName="Username"  
                            SortExpression="Username">

<ItemTemplate> 
         <asp:Label ID="lblUsername" runat="server" Text='<%# Eval("Username") %>' /> 
        </ItemTemplate> 
</telerik:GridTemplateColumn> 

And here is the code behind:

    protected void MembersGrid_ItemDataBound(object sender, GridItemEventArgs e) 
    { 
        if ((e.Item is GridDataItem))  
        { 
            GridDataItem dataItem = (GridDataItem)e.Item; 
            string username = dataItem["Username"].Text; 
 
            ImageButton btnEditUser = (ImageButton)e.Item.FindControl("EditUserImgBtn"); 
            btnEditUser.Attributes["href"] = "#"
            btnEditUser.Attributes["onclick"] = String.Format("return ShowEditUser('{0}', '{1}');", username, e.Item.ItemIndex); 
    } 
    } 

I need to get the value of the GridTemplateColumn ("Username") into the ShowEditUser('{0}', '{1}');", username, e.Item.ItemIndex), can someone please offer some advice this would be much appreciated.

Do I need to use a GridBoundColumn instead of the above? I don't think that would make any difference though.

Thank you in advance for help :)

3 Answers, 1 is accepted

Sort by
0
Accepted
Radoslav
Telerik team
answered on 14 May 2010, 03:12 PM
Hi Grant,

Could you please try the following code snippet:
protected void MembersGrid_ItemDataBound(object sender, GridItemEventArgs e)
{
    if ((e.Item is GridDataItem))
    {
        GridDataItem dataItem = (GridDataItem)e.Item;
        Label usernameLabel = dataItem["Username"].FindControl("lblUsername") as Label;
        string username = usernameLabel.Text;
 
        ImageButton btnEditUser = (ImageButton)e.Item.FindControl("EditUserImgBtn");
        btnEditUser.Attributes["href"] = "#";
        btnEditUser.Attributes["onclick"] = String.Format("return ShowEditUser('{0}', '{1}');", username, e.Item.ItemIndex);
     }
}

For more information on how to access cells and rows please check out the following online documentation article:
http://www.telerik.com/help/aspnet-ajax/grdaccessingcellsandrows.html

I hope this helps.

Greetings,
Radoslav
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Accepted
Shinu
Top achievements
Rank 2
answered on 14 May 2010, 03:13 PM

Hello Grant,

One method that you can try is accessing the Label control and get the value by accessing the Text property.

C#:

 
    protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)  
    {  
        if ((e.Item is GridDataItem))  
        {  
            GridDataItem dataItem = (GridDataItem)e.Item;  
            Label lbl = (Label)dataItem.FindControl("lblUsername");  
            string username = lbl.Text;         
        }  
    } 

-Shinu.

0
Mike
Top achievements
Rank 2
answered on 14 May 2010, 09:39 PM
Thanks Radoslav and Shinu

It was 2:30am after an 18 hour day and now I feel so stupid because the answer is quite obvious. I have been using telerik controls for quite a while now and I must have been suffering from a brain lapse or something sheeesh, sorry to waste your time guys but I guess this may help someone new to using the controls.

Cheers ;)
Tags
Grid
Asked by
Mike
Top achievements
Rank 2
Answers by
Radoslav
Telerik team
Shinu
Top achievements
Rank 2
Mike
Top achievements
Rank 2
Share this question
or