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

get datakey value outside of radgrid event

15 Answers 1448 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ranro
Top achievements
Rank 1
ranro asked on 15 Jun 2008, 07:32 PM

I was wondering if someone could explain to me how to get the datakeyvalue of a selected row on the server side and outside of a radgrid event. 

For instance say I have a radgrid and it has a row that has been selected.  When someone clicks a button (called btnTest for instance) that is somewhere else on the page, how do I go about getting the datakeyvalue of the selected row in btnTest’s click event.

15 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 16 Jun 2008, 05:20 AM
Hi Ranro,

Try the following code snippet to achieve the desired scenario.

ASPX:
<ClientSettings > 
   <Selecting  AllowRowSelect="true" /> 
 </ClientSettings> 
<MasterTableView  DataKeyNames="CustomerID"    > 
 

CS:
 protected void Button1_Click(object sender, EventArgs e) 
    { 
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items) 
        { 
            if (item.Selected) 
            { 
                string strKey = item.GetDataKeyValue("CustomerID").ToString(); 
            } 
        } 
    } 


Thanks
Shinu.

0
Kel Way
Top achievements
Rank 1
answered on 22 Oct 2008, 04:12 PM
How can I get the selected row's datakey value using js on client-side?

 I have already found this snippet in the help file:

 

var firstDataItem = $find("<%= rgGrid1.ClientID %>").get_masterTableView().get_dataItems()[0];

 

 

var keyValues = firstDataItem.getDataKeyValue("unique_id");

This does not get SELECTED row's data key, only first row.

I need to get this outside of a radgrid client-side event as well.
thanks.

 

0
Daniel
Telerik team
answered on 22 Oct 2008, 04:57 PM
Hello Kel,

Please examine the approach shown below:
function buttonClick() 
    var radgrid = $find('<%= RadGrid1.ClientID %>'); 
    alert(radgrid.get_masterTableView().get_selectedItems()[0].getDataKeyValue("OrderID")); 

Kind regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Francis Frank
Top achievements
Rank 1
answered on 25 Oct 2011, 11:03 AM
Please I need some help here friends!

I have a grid and 3 linkbuttons inside it then i want to trigger seperate events when each of the butttons are clicked not when the row is selected.
I have been able to get the buttons respond on sepearate clicks but i could retrieve any value from that row neither could I acces the clientdatakeynames of the grid

I will be glad to have a response

waiting sirs ...
Thanks in anticipation
0
Jayesh Goyani
Top achievements
Rank 2
answered on 25 Oct 2011, 12:05 PM
Hello,

     
<MasterTableView  DataKeyNames="ID" >
protected void btn1_Click(object sender, EventArgs e)
    {
        Button btn1 = sender as Button;
        GridDataItem item = btn1.NamingContainer as GridDataItem;
        string ID = item.GetDataKeyValue("ID").ToString();
    }

Let me know if any concern.


Thanks,
Jayesh Goyani
0
Princy
Top achievements
Rank 1
answered on 25 Oct 2011, 12:15 PM
Hello Francis,

You can try the following method to access ClientDataKeyValues in onclick event of LinkButton.
C#:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
    GridDataItem item = (GridDataItem)e.Item;
    LinkButton link = (LinkButton)item.FindControl("LinkButton1");
    int rowindex = item.ItemIndex;
    link.Attributes.Add("onclick", "clicked('" + rowindex + "');");
 }
}

Javascript:
function clicked(rowindex)
{
   var row = $find('<%= RadGrid1.ClientID %>').get_masterTableView().get_dataItems()[rowindex];
   alert(row.getDataKeyValue("ID"));//DataKeyName
}

Thanks,
Princy.
0
Francis Frank
Top achievements
Rank 1
answered on 28 Oct 2011, 02:43 PM
Thanks Princy

This Fantastic Code just bailed me out

protected void getAcceptRequest_Button_Click(object sender, System.EventArgs e)
{
LinkButton butAccept = (LinkButton)sender;
GridDataItem editItem = (GridDataItem)butAccept.NamingContainer;
HiddenField hfRequest_Id = (HiddenField)editItem.FindControl("hfRequestId");
string requestId = hfRequest_Id.Value;

HiddenField hfRequest_From = (HiddenField)editItem.FindControl("hfRequestFrom");
string requestFrom = hfRequest_From.Value;

HiddenField hfRequest_To = (HiddenField)editItem.FindControl("hfRequestTo");
string requestTo = hfRequest_To.Value;

runAccept(requestId, requestFrom, requestTo);
}

I appreciate!


0
Prassin
Top achievements
Rank 1
answered on 26 Jul 2012, 09:15 AM
Hi All,

Is that possible to add multiple datakeys on a Rad Grid.. please give the code

Regards,

Prassin
0
Shinu
Top achievements
Rank 1
answered on 26 Jul 2012, 09:25 AM
Hi,

Please try setting the DataKeyNames property of the MasterTableView as given below.

ASPX:
<telerik:RadGrid ID="Rad" runat="server" AllowFilteringByColumn="true" DataSourceID="SqlDataSource1" AutoGenerateColumns="false" >
    <MasterTableView DataKeyNames="OrderID,ShipName">
         . . .
    </MasterTableView>

Thanks,
Shinu.
0
Deyza
Top achievements
Rank 1
answered on 23 Oct 2012, 08:20 PM
Gracias Daniel. Muy Utíl. A mi me funciono. 
Thank you  Daniel. Very useful. Worked for me.
0
Awais
Top achievements
Rank 1
answered on 14 Sep 2013, 05:43 AM
@Jayesh Goyani 

Object reference not set to an instance of an object.

what should i do now ??
0
Jayesh Goyani
Top achievements
Rank 2
answered on 16 Sep 2013, 06:47 AM
Hello,

Can you please elaborate your scenario / provide you code?

Thanks,
Jayesh Goyani
0
THANGARAI
Top achievements
Rank 1
answered on 16 Apr 2014, 08:05 AM
Hi Princy,

I couldn't get the DataKeyValue at client side using RoeIndex.

var r=$find('<%= Rg_Centers.ClientID%>').get_masterTableView().get_dataItems()[index];
            alert(r.getDataKeyValue("CenterId"));

the above code gives null value.



0
Princy
Top achievements
Rank 1
answered on 16 Apr 2014, 08:19 AM
Hi Thangarai,

Please make sure you have set the ClientDataKeyNames for the Grid.

ASPX:
<MasterTableView ClientDataKeyNames="CenterId"  DataKeyNames="CenterId">

Thanks,
Princy
0
THANGARAI
Top achievements
Rank 1
answered on 15 Jul 2014, 06:47 AM
Thanks Princy,

Its working fine.
Tags
Grid
Asked by
ranro
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Kel Way
Top achievements
Rank 1
Daniel
Telerik team
Francis Frank
Top achievements
Rank 1
Jayesh Goyani
Top achievements
Rank 2
Princy
Top achievements
Rank 1
Prassin
Top achievements
Rank 1
Deyza
Top achievements
Rank 1
Awais
Top achievements
Rank 1
THANGARAI
Top achievements
Rank 1
Share this question
or