Identifying row selected by a GridButtonColumn click client side

4 posts, 0 answers
  1. Cory Benjamin
    Cory Benjamin avatar
    18 posts
    Member since:
    Jul 2006

    Posted 24 Sep 2010 Link to this post

    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.
  2. Cory Benjamin
    Cory Benjamin avatar
    18 posts
    Member since:
    Jul 2006

    Posted 24 Sep 2010 Link to this post

    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") + "')");
                    }
                }
    }

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 27 Sep 2010 Link to this post

    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.
  5. Cory Benjamin
    Cory Benjamin avatar
    18 posts
    Member since:
    Jul 2006

    Posted 10 Oct 2010 Link to this post

    Thanks :)
Back to Top