Fetch DatakeyValue of particular row on Radiobbutton click inside RadGrid

5 posts, 0 answers
  1. Priyanka
    Priyanka avatar
    34 posts
    Member since:
    Dec 2014

    Posted 11 Dec 2014 Link to this post

    Hi
    I have a Radiobutton next to every row of RadGrid.
    I am trying to access the datakeyvalue of a selected row using radiobutton, i.e., when particular row's Radiobutton of RadGrid is checked, then on 'CheckedChanged' event of RadioButton, I am trying to get the datakey value of that row inside a label or variable.

    aspx page Code:
    <telerik:RadGrid ID="RadGrid1" EnableViewState="false" runat="server" AllowPaging="true"
                            AllowSorting="False" AllowFilteringByColumn="true" GridLines="None" OnInit="RadGrid1_Init" OnDeleteCommand="RadGrid1_DeleteCommand">
                            <ItemStyle Wrap="false"></ItemStyle>
                            <MasterTableView AllowMultiColumnSorting="true" TableLayout="Fixed" DataKeyNames="Id" ClientDataKeyNames="Id">
                                <Columns>
                                    <telerik:GridNumericColumn DataField="Id" HeaderText="Id" HeaderStyle-Width="100px" AllowFiltering="False"
                                        FilterControlWidth="50px">
                                    </telerik:GridNumericColumn>
                                    <telerik:GridBoundColumn DataField="Description" HeaderText="Description" AndCurrentFilterFunction="Contains" FilterListOptions="AllowAllFilters">
                                    </telerik:GridBoundColumn>
    <telerik:GridTemplateColumn UniqueName="" HeaderText="Select">
                                        <ItemTemplate>
                                            <asp:RadioButton ID="rbdSelect" runat="server" AutoPostBack="true" OnCheckedChanged="rbdSelect_CheckedChanged"></asp:RadioButton>
                                        </ItemTemplate>
                                    </telerik:GridTemplateColumn>
                                </Columns>
                            </MasterTableView>
    .cs page code:
    protected void rbdSelect_CheckedChanged(object sender, EventArgs e)
            {
                foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
                {
                    RadioButton rbd = (RadioButton)item.FindControl("rbdSelect");
                    if (rbd.Checked==true)
                    {
                        string key = item.GetDataKeyValue("Id").ToString();
                        //string id = RadGrid1.MasterTableView.Items[0].GetDataKeyValue("Id").ToString();
                    }
                }
                Response.Redirect("~/PrintInvoice.aspx");
            }
    but unable to get the DataKeyValue of selected row.

    I also want to call the selected row's DataKeyValue on another page(say Default.aspx), to show the particuar RadGrid row's record on other page(say Default.aspx)

    Please help me what is missing in my code?

  2. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 15 Dec 2014 Link to this post

    Hi Priyanka,

    I have tested your current implementation and the "Id" is correctly retrieved on my end.

    However, I have noticed that you are traversing all items in the grid. In your scenario you can use the RadioButton and its NamingContainer property, which will return the GridDataItem:
    protected void rbdSelect_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton rbd = sender as RadioButton;
        GridDataItem currentItem = rbd.NamingContainer as GridDataItem;
     
        if (rbd.Checked == true)
        {
            string key = currentItem.GetDataKeyValue("Id").ToString();
        }
    }

    Following is the entire example that works correctly on my end:
    <telerik:RadGrid ID="RadGrid1" EnableViewState="false" runat="server" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource"
        AllowSorting="False" AllowFilteringByColumn="true" GridLines="None">
        <ItemStyle Wrap="false"></ItemStyle>
        <MasterTableView AllowMultiColumnSorting="true" TableLayout="Fixed" DataKeyNames="Id" ClientDataKeyNames="Id">
            <Columns>
                <telerik:GridNumericColumn DataField="Id" HeaderText="Id" HeaderStyle-Width="100px" AllowFiltering="False"
                    FilterControlWidth="50px">
                </telerik:GridNumericColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" AndCurrentFilterFunction="Contains" FilterListOptions="AllowAllFilters">
                </telerik:GridBoundColumn>
                <telerik:GridTemplateColumn UniqueName="" HeaderText="Select">
                    <ItemTemplate>
                        <asp:RadioButton ID="rbdSelect" runat="server" AutoPostBack="true" OnCheckedChanged="rbdSelect_CheckedChanged"></asp:RadioButton>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

    And the code-behind:
    public DataTable GetData()
    {
        DataTable table = new DataTable();
        table.Columns.Add("Id", typeof(int));
        table.Columns.Add("Description", typeof(string));
        for (int i = 0; i < 5; i++)
        {
            table.Rows.Add(i, "Description");
        }
     
        return table;
    }
     
    protected void rbdSelect_CheckedChanged(object sender, EventArgs e)
    {
        RadioButton rbd = sender as RadioButton;
        GridDataItem currentItem = rbd.NamingContainer as GridDataItem;
     
        if (rbd.Checked == true)
        {
            string key = currentItem.GetDataKeyValue("Id").ToString();
        }
    }
     
    protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        (sender as RadGrid).DataSource = GetData();
    }

    Hope this helps.


    Regards,
    Konstantin Dikov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Priyanka
    Priyanka avatar
    34 posts
    Member since:
    Dec 2014

    Posted 16 Dec 2014 in reply to Konstantin Dikov Link to this post

    @Konstantin: Thank you for the reply.

    I found the Issue of my problem. Since I am binding data inside "RadGrid" through "WebService", so Gird is getting data at client side. In this case there is no viewstate to hold the records in grid at server end.

    Now I need to know How to fetch datakeyvalue using Javscript ?
    Scenario is same: I need to select row using Radiobutton and need to fetch the selected Row's Id on page because I had to pass the selected Id on another page (say Default2.aspx)

    Any help will be appreciated. Please reply
  5. Priyanka
    Priyanka avatar
    34 posts
    Member since:
    Dec 2014

    Posted 16 Dec 2014 Link to this post

    Or Is it possible to fetch datakeyname (using javscript) of selected row uisng:

    <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn"></telerik:GridClientSelectColumn>

    I need to save this datakeyname on page to pass iton another page.

    Please help me.

  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1798 posts

    Posted 18 Dec 2014 Link to this post

    Hello Priyanka,

    Within the client-side OnRowSelected event you can get the dataItem from the event arguments and use the getDataKeyValue(key) method for extracting the value:
    <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
        <script type="text/javascript">
            function rowSelected(sender, args) {
                var dataItem = args.get_gridDataItem();
                var id = dataItem.getDataKeyValue("Id");
            }
        </script>
    </telerik:RadCodeBlock>
     
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="true" OnNeedDataSource="RadGrid1_NeedDataSource"
        AllowSorting="False" AllowFilteringByColumn="true" GridLines="None" AutoGenerateColumns="false">           
        <MasterTableView AllowMultiColumnSorting="true" TableLayout="Fixed" DataKeyNames="Id" ClientDataKeyNames="Id">
            <Columns>
                <telerik:GridNumericColumn DataField="Id" HeaderText="Id" HeaderStyle-Width="100px" AllowFiltering="False"
                    FilterControlWidth="50px">
                </telerik:GridNumericColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" AndCurrentFilterFunction="Contains" FilterListOptions="AllowAllFilters">
                </telerik:GridBoundColumn>
                <telerik:GridClientSelectColumn></telerik:GridClientSelectColumn>
            </Columns>
        </MasterTableView>
        <ClientSettings>
            <Selecting AllowRowSelect="true" UseClientSelectColumnOnly="true"/>
            <ClientEvents OnRowSelected="rowSelected" />
        </ClientSettings>
    </telerik:RadGrid>

    You should only keep in mind that the data field must be included in the ClientDataKeyNames collection of the MasterTableView.


    Best Regards,
    Konstantin Dikov
    Telerik
     

    Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

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