get datakey value outside of radgrid event

16 posts, 0 answers
  1. ranro
    ranro avatar
    50 posts
    Member since:
    Aug 2007

    Posted 15 Jun 2008 Link to this post

    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.

  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 16 Jun 2008 Link to this post

    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.

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Kel Way
    Kel Way avatar
    5 posts
    Member since:
    Jul 2008

    Posted 22 Oct 2008 Link to this post

    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.

     

  5. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 22 Oct 2008 Link to this post

    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.
  6. Francis Frank
    Francis Frank avatar
    35 posts
    Member since:
    Mar 2010

    Posted 25 Oct 2011 Link to this post

    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
  7. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 25 Oct 2011 Link to this post

    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
  8. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Oct 2011 Link to this post

    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.
  9. Francis Frank
    Francis Frank avatar
    35 posts
    Member since:
    Mar 2010

    Posted 28 Oct 2011 Link to this post

    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!


  10. Prassin
    Prassin avatar
    87 posts
    Member since:
    Jun 2012

    Posted 26 Jul 2012 Link to this post

    Hi All,

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

    Regards,

    Prassin
  11. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 26 Jul 2012 Link to this post

    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.
  12. Deyza
    Deyza avatar
    1 posts
    Member since:
    Oct 2012

    Posted 23 Oct 2012 Link to this post

    Gracias Daniel. Muy Utíl. A mi me funciono. 
    Thank you  Daniel. Very useful. Worked for me.
  13. Awais
    Awais avatar
    1 posts
    Member since:
    Sep 2013

    Posted 14 Sep 2013 Link to this post

    @Jayesh Goyani 

    Object reference not set to an instance of an object.

    what should i do now ??
  14. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 16 Sep 2013 Link to this post

    Hello,

    Can you please elaborate your scenario / provide you code?

    Thanks,
    Jayesh Goyani
  15. THANGARAI
    THANGARAI avatar
    21 posts
    Member since:
    Nov 2013

    Posted 16 Apr 2014 in reply to Princy Link to this post

    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.



  16. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 16 Apr 2014 in reply to THANGARAI Link to this post

    Hi Thangarai,

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

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

    Thanks,
    Princy
  17. THANGARAI
    THANGARAI avatar
    21 posts
    Member since:
    Nov 2013

    Posted 15 Jul 2014 in reply to Princy Link to this post

    Thanks Princy,

    Its working fine.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017