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

Button within RadGrid column

2 Answers 139 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 20 Dec 2011, 10:33 PM
I have a class that inherits from GridBoundColumn (TextFilterButtonColumn.cs) in order to add some custom filtering. I then use TextFilterButtonColumn to create columns within a RadGrid. I would like to add a click event to each item in the column. I've done the following within TextFilterButtonColumn.cs:

public override void Initialize()
{
    base.Initialize();
    Owner.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "Common.js");
}
public override void PrepareCell(TableCell cell, GridItem item)
{
    base.PrepareCell(cell, item);
    if (!(item is GridDataItem)) return;
    var clickScript = string.Format("javascript:tfbc_Click('{0}','{1}');", this.Owner.ClientID, CommandName);
    var linkButton = new HtmlAnchor() { ID = "lnk", InnerText = cell.Text, HRef = clickScript };
    cell.Controls.Clear();
    cell.Controls.Add(linkButton);
}

In the Common.js file I have the following function:

function tfbc_Click(owner, command, argument) {
    $find(owner).fireCommand(command, argument);
}

This click event is then accessed inside of the RadGrid ItemCommand event by setting the CommandName property of the GridBoundColumn to "commandName" and checking for it:

if(e.CommandName == "commandName")
{
    var item = (GridDataItem) e.Item;
    var strKey = item.GetDataKeyValue("Id").ToString();
    var control = (Details)Page.LoadControl("~/Controls/Details.ascx");
    control.Id = long.Parse(strKey);
    MPS.AddPopup(control);
}

I was hoping to access the Id of the row that the radgrid is bound to by calling GetDataKeyValue however this always returns 1. Is there a way to access information regarding the datasource for that row in the ItemCommand event? Or is there another way to accomplish this? Thanks.

2 Answers, 1 is accepted

Sort by
0
Accepted
Mira
Telerik team
answered on 23 Dec 2011, 03:49 PM
Hello,

Please use the following code in order to access the corresponding grid item:
public override void PrepareCell(TableCell cell, GridItem item)
{
    base.PrepareCell(cell, item);
    if (!(item is GridDataItem)) return;
    var clickScript = string.Format("javascript:tfbc_Click('{0}','{1}', '{2}');", this.Owner.ClientID, CommandName, item.ItemIndex);
    var linkButton = new HtmlAnchor() { ID = "lnk", InnerText = cell.Text, HRef = clickScript };
    cell.Controls.Clear();
    cell.Controls.Add(linkButton);
}

if (e.CommandName == "commandName")
{
    var item = RadGrid2.MasterTableView.Items[int.Parse(e.CommandArgument)];
    //..
}

I hope this helps.

Kind regards,
Mira
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Joseph
Top achievements
Rank 1
answered on 23 Dec 2011, 04:18 PM
Thanks for the reply.
Tags
Grid
Asked by
Joseph
Top achievements
Rank 1
Answers by
Mira
Telerik team
Joseph
Top achievements
Rank 1
Share this question
or