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

Working with RadGrid and TemplateColumns in ItemCommand

1 Answer 294 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steve
Top achievements
Rank 1
Steve asked on 25 Jul 2011, 06:43 PM
Telerik,

Im trying to get the value of a TemplateColumn item when the ItemCommand event is raised. My TemplateColumn looks like the following:


<telerik:GridTemplateColumn HeaderText="User" UniqueName="User" Reorderable="false" Display="true" SortExpression="User">
    <ItemTemplate>    
        <asp:LinkButton ID="btnButton1" runat="server" ToolTip="Edit this user" CommandName="EditUser" Visible="<%# Master.UserPrefs.HasEditRights %>"><%# Eval("User") %></asp:LinkButton>                         
        <asp:Label ID="lblLabel1" runat="server" Visible="<%#!Master.UserPrefs.HasEditRights %>"><%# Eval("User") %></asp:Label>
    </ItemTemplate>                   
</telerik:GridTemplateColumn>

Basically, the template is built using a boolean value (determining if the current user logged in has EditRights to the record) to either display a LinkButton (to launch a custom edit screen) or to display a Label (if the current user doesn't have the appropriate permission).

In the ItemCommand, I would like to get the value of the data bound to either the Label or the LinkButton. The closest I got to getting the information is by doing something like the following:

protected void gvUsers_ItemCommand(object sender, GridCommandEventArgs e)
{
        RadGrid grid = (RadGrid)sender;
        switch (e.CommandName.ToUpper())
        {
            case "MyCommandName":               
                foreach (GridDataItem item in grid.MasterTableView.Items)
                {                   
                string cellContent = ((GridLinkButton)item.Cells[3].Controls[0]).Text
                }
      break;
        }
}

Is this the only way to get the Text value of the GridLinkButton? Can you please provide any other information for retrieving the values associated with a TemplateColumn using the markup in my Template?

Thanks

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 26 Jul 2011, 05:43 AM
Hello Steve,

Try the following code snippet to get the requirement.
C#:
protected void RadGrid1_ItemCommand(object sender, Telerik.Web.UI.GridCommandEventArgs e)
   {
        if (e.CommandName == "EditUser")
        {
               GridDataItem Item = (GridDataItem)e.Item;
               LinkButton linkbtn = (LinkButton)e.Item.FindControl("btnButton1");
               linkbtn.Text = "EmployeeID";
        }
   }

Thanks,
Shinu.
Tags
Grid
Asked by
Steve
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or