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

Get cell value from column click

2 Answers 258 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 09 May 2012, 09:21 PM
Hi,

Here is what I am trying to do. I have a grid and I want to add a column that would look like a hyperlink (linkbutton). When I click on it, I want to launch a javascript and get the data of a cell from the same row.

Here is what I have so far:

<telerik:GridButtonColumn Text="Détail" UniqueName="D" ItemStyle-HorizontalAlign="Center"
                            ButtonType="LinkButton" CommandName="CustomDetail"  />

I added the client event to fire the onCommand javascript and intercept my custom command and it works. So far so good. My problem is that I can't get the cells value from the same row because I do not know which row has been clicked. I know how to do this if the row had been selected, but it is not the case in this example.

Thanks

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 10 May 2012, 05:19 AM
Hi Daniel,

Please take a look into the following code snippet I tried to get the data from a cell when I click on the GridButtonColumn of the same row.

ASPX:
<telerik:GridButtonColumn Text="Détail" UniqueName="D" ItemStyle-HorizontalAlign="Center" ButtonType="LinkButton" CommandName="CustomDetail"  />

C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
    if (e.Item is GridDataItem)
    {
       int index = e.Item.ItemIndex; //Get the index of the Row
       GridDataItem ditem = (GridDataItem)e.Item;
       LinkButton LinkButton1= (LinkButton)ditem["D"].Controls[0];
       LinkButton1.Attributes.Add("onclick", "OnClick('" + index + "')"); //Passing index to the Javascript
    }
}

Javascript:
<script type="text/javascript">
function OnClick(index)
{
    var master = $find("<%=RadGrid1.ClientID %>").get_masterTableView();
    var row = master.get_dataItems()[index]; //accessing row with the Row index
    var cell = master.getCellByColumnUniqueName(row, "UniqueName"); // Accessing the cell using its UniqueName
    alert(cell.innerHTML);
}
</script>

Hope this helps.

Thanks,
Shinu.
0
Daniel
Top achievements
Rank 1
answered on 10 May 2012, 03:00 PM
thanks
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Daniel
Top achievements
Rank 1
Share this question
or