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

Identifying row selected by a GridButtonColumn click client side

3 Answers 539 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Cory Benjamin
Top achievements
Rank 2
Cory Benjamin asked on 24 Sep 2010, 08:08 PM
I have a "Select Record" GridButtonColumn inside my grid.  I am hooking into the onclick event of that button to call a Javascript method, which works fine.  But in that method, I can't figure out how to determine which row was clicked.  SelectedItems is empty because my Javascript is being called before the grid selects the row.

And I can't use the onRowSelected event because I want my logic to occur only when they click on my grid button, not if they simply select a row by clicking the row in any area outside of my button.

In the server-side "ItemCreated" event, where my JavaScript call is constructed, I try passing the ClientRowIndex property of the GridButtonColumn data item, but it's always -1.

if (e.Item is GridDataItem)
{
    LinkButton lb = (LinkButton)(((GridDataItem)e.Item)["SelectRecord"].FindControl("ctl00"));
    lb.Attributes.Add("onclick", "return onSelectClient('" + LookupGrid.ClientID + "'," + e.Item.ClientRowIndex + ")");
}

Thanks.

3 Answers, 1 is accepted

Sort by
0
Cory Benjamin
Top achievements
Rank 2
answered on 24 Sep 2010, 09:57 PM
While I would still love to know the answer to this, I have solved my immediate problem.  Instead of relying on Javascript code to extract the fields I need from the selected row, I am passing the needed fields as parameters to my Javascript method.

void LookupRadGrid1_ItemCreated(object sender, GridItemEventArgs e)
    {
            if (e.Item is GridDataItem)
            {
                LinkButton lb = (LinkButton)(((GridDataItem)e.Item)["SelectRecord"].FindControl("ctl00"));
                if (lb != null)
                {
                    lb.Attributes.Add("onclick", "return onSelectClient('" + ((GridDataItem)e.Item).GetDataKeyValue("Id") + "','" + ((GridDataItem)e.Item).GetDataKeyValue("Desc") + "')");
                }
            }
}

0
Princy
Top achievements
Rank 2
answered on 27 Sep 2010, 06:51 AM
Hello Cory,

You could use ItemIndex than ClientRowIndex to pass the row index to client side event handler.

ASPX:
<telerik:RadGrid . . . . . .>
  <MasterTableView DataKeyNames="Id,Desc" ClientDataKeyNames="Id,Desc">

C#:
protected void LookupRadGrid1_ItemCreated(object sender, GridItemEventArgs e)
   {
       if (e.Item is GridDataItem)
       {
           GridDataItem item = (GridDataItem)e.Item;
           LinkButton lb = (LinkButton)item["SelectRecord"].Controls[0];
           lb.Attributes.Add("onclick", "onSelectClient('" + item.ItemIndex + "');");
       }
   }

In client side event handler using that index access the row and get the DataKeyValue like below.

Java Script:
<script type="text/javascript">
   function onSelectClient(index) {
        var grid = $find("<%=LookupRadGrid1.ClientID %>");
        var MasterTable = grid.get_masterTableView();
        var row = MasterTable.get_dataItems()[index];
        var Id=row.getDataKeyValue("Id") //access DataKeyValue
        var Desc=row.getDataKeyValue("Desc")   //access DataKeyValue
    }  
 
</script>


Thanks,
Princy.
0
Cory Benjamin
Top achievements
Rank 2
answered on 10 Oct 2010, 06:51 AM
Thanks :)
Tags
Grid
Asked by
Cory Benjamin
Top achievements
Rank 2
Answers by
Cory Benjamin
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or