Can't get selected items on server side in a Radgrid with checkbox

12 posts, 1 answers
  1. PPRINC
    PPRINC avatar
    9 posts
    Member since:
    Jun 2013

    Posted 18 Jun 2013 Link to this post

    Hello Dears,

    I am facing an issue.

    After upgrading from telerik v2009  to  telerk v2012 , I can't get my selected rows values in a radgrid with checkboxes from code behind

    here is the code in the markup
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowPaging="True"
                              PageSize="5"     Width="100%"    OnPageIndexChanged="RadGrid1_PageIndexChanged"
                                GridLines="None" Skin="Vista" AllowMultiRowSelection="True"  >
                                <MasterTableView >
                                <Columns>
                                    <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" ></telerik:GridClientSelectColumn>
                                </Columns>
                                    <RowIndicatorColumn>
                                        <HeaderStyle Width="20px" />
                                    </RowIndicatorColumn>
                                    <ExpandCollapseColumn>
                                        <HeaderStyle Width="20px" />
                                    </ExpandCollapseColumn>
                                    <PagerStyle Mode="NextPrev" />
                                </MasterTableView>
                                <HeaderStyle Font-Bold="True" Font-Italic="False" Font-Overline="False"
                                    Font-Strikeout="False" Font-Underline="False" Wrap="True" />
                                <ClientSettings EnableRowHoverStyle="true"  >
                                    <Selecting AllowRowSelect="True" UseClientSelectColumnOnly "true"   />
                                </ClientSettings>
                            </telerik:RadGrid>
    and now the code behind

     protected void  Ok_Click(object sender, System.EventArgs e)
            {
    foreach (GridDataItem item in RadGrid1.SelectedItems)
                   {
                       CheckBox chk = (CheckBox)item["ClientSelectColumn"].Controls[0];
                       id = item["ID"].Text; // get the id with its column name
                       name = item["NAME"].Text; // get the name with its column name
                       chec = chk.Checked;
     
     
                   }
    }

    It doesn't even enter in the loop.
    What can I do to resolve it ?

    Thank in advance for your assistance
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 19 Jun 2013 Link to this post

    Hi,

    I guess you want the selected row on a button click.Please try the code given below.

    ASPX:
    <telerik:RadGrid ID="RadGrid1" runat="server" AllowMultiRowSelection="True">
       <MasterTableView DataKeyNames="OrderID">
          <Columns>
              <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn"></telerik:GridClientSelectColumn>
         </Columns>
                 <RowIndicatorColumn>
                       <HeaderStyle Width="20px" />
                 </RowIndicatorColumn>
                <ExpandCollapseColumn>
                      <HeaderStyle Width="20px" />
                 </ExpandCollapseColumn>
                <PagerStyle Mode="NextPrev" />
       </MasterTableView>
         <HeaderStyle Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Strikeout="False"
                   Font-Underline="False" Wrap="True" />
        <ClientSettings EnableRowHoverStyle="true">
              <Selecting AllowRowSelect="True" UseClientSelectColumnOnly="true" />
        </ClientSettings>
    </telerik:RadGrid>
    <asp:Button ID="Button1" runat="server" Text="Ok" onclick="Button1_Click" />

    C#:
    protected void Button1_Click(object sender, EventArgs e)
    {
      foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
       {
         CheckBox chk = (CheckBox)item["ClientSelectColumn"].Controls[0];
          if (chk.Checked)
           {
            string value = item["OrderID"].Text;// access the cell value using ColumnUniqueName
            Response.Write("<script language='javascript'>alert('" + value + " .');</script>");
           }
        }
    }

    Thanks,
    Princy
  3. PPRINC
    PPRINC avatar
    9 posts
    Member since:
    Jun 2013

    Posted 19 Jun 2013 Link to this post

    Aleardy found!

    As i'm using master pages, I just needed to ad add a radAjaxManager on my master page and a radAjaxManagerProxy on the content page to get it fixed. and that's done.

    Thanks anyway,

    Regards.
  4. PPRINC
    PPRINC avatar
    9 posts
    Member since:
    Jun 2013

    Posted 19 Jun 2013 Link to this post

    Thanks for your support Princy.

    Best regards,
  5. Salim
    Salim avatar
    10 posts
    Member since:
    Apr 2012

    Posted 30 Dec 2015 Link to this post

    Your quote goes here

    Hi Princy, 

    I tried your code but the checkbox has always property .checked = False no matter what (even if I check it)

  6. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2037 posts

    Posted 01 Jan 2016 Link to this post

    Hi Salim,

    The example will not work if you are using Simple Data-Binding and if you are calling the DataBind method on each postback. Please use the Advanced Data-Binding instead and see if the approach will work:

    Regards,
    Konstantin Dikov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  7. Salim
    Salim avatar
    10 posts
    Member since:
    Apr 2012

    Posted 18 Jan 2016 in reply to Konstantin Dikov Link to this post

    I'm already using the advanced data binding but it doesn't work.
  8. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    2037 posts

    Posted 19 Jan 2016 Link to this post

    Hello Salim,

    Could you please open a regular support ticket and attach a sample page demonstrating the issue in question?

    Meanwhile you can ensure that you are not disabling the ViewState of the controls and that you are not calling DataBind or Rebind methods, before you try to retrieve the CheckBox value.


    Regards,
    Konstantin Dikov
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Dave
    Dave avatar
    2 posts
    Member since:
    Jun 2010

    Posted 12 Jan in reply to Salim Link to this post

    Salim, did you ever get a resolution on this?  Having the same problem.

    -Dave

  10. Dave
    Dave avatar
    2 posts
    Member since:
    Jun 2010

    Posted 12 Jan in reply to Dave Link to this post

    Nevermind...if I place my databind in Page_PreRender instead of Page_Load it works.  I couldn't find any obvious solution to this...just trial and error.
  11. yook
    yook avatar
    4 posts
    Member since:
    Mar 2017

    Posted 13 Mar Link to this post

    Hi,
    I have a rad grid with paging enabled.I select all rows in multiple pages using a GridClientSelectColumn. When I click on print button it prints only the records which are displayed in first page,not all records selected in multiple pages. How to fix it. Plz help...

    This is how i selected all checkboxes in multiple pages.
     var selected = {};
                    function radGridWarehouseUtility_RowSelected(sender, args) {
                        var LID = args.getDataKeyValue("LID");
                        if (!selected[LID]) {
                            selected[LID] = true;
                        }
                    }

                    function radGridWarehouseUtility_RowDeselected(sender, args) {
                        var LID = args.getDataKeyValue("LID");
                        if (selected[LID]) {
                            selected[LID] = null;
                        }
                    }

                   function pageLoad() {
                       var dataItems = $find('<%=radGridWarehouseUtility.ClientID %>').get_masterTableView().get_dataItems();
                       for (var i = 0, j = dataItems.length; i < j; i++) {
                           var item = dataItems[i];
                           if (selected[item.getDataKeyValue("LID")]) {
                               item.set_selected(true);
                           }
                       }
                   }

    <ClientSettings EnablePostBackOnRowClick="true">
    <Selecting AllowRowSelect="True"></Selecting>
    <ClientEvents OnRowSelected="radGridWarehouseUtility_RowSelected"
    OnRowDeselected="radGridWarehouseUtility_RowDeselected" />
    </ClientSettings>

    <telerik:GridClientSelectColumn UniqueName="Select" />

    code behind...

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click
     For Each item As GridDataItem In radGridWarehouseUtility.SelectedItems
                Dim item1 As GridDataItem = item
                Dim chkBox As CheckBox
                chkBox = DirectCast(item1.Item("Select").Controls(0), CheckBox)

                If chkBox.Enabled Then
                    Dim lid As String
                    lid = CType(radGridWarehouseUtility.MasterTableView.DataKeyValues(item.ItemIndex)("LID"), String)
                    qryParams = qryParams & lid & ","
                End If
            Next
    End Sub

    Thanks
  12. yook
    yook avatar
    4 posts
    Member since:
    Mar 2017

    Posted 13 Mar Link to this post

    Hi,
    I have a rad grid with paging enabled.I select all rows in multiple pages using a GridClientSelectColumn. When I click on print button it prints only the records which are displayed in first page,not all records selected in multiple pages. How to fix it. Plz help...

    This is how i selected all checkboxes in multiple pages.
     var selected = {};
                    function radGridWarehouseUtility_RowSelected(sender, args) {
                        var LID = args.getDataKeyValue("LID");
                        if (!selected[LID]) {
                            selected[LID] = true;
                        }
                    }

                    function radGridWarehouseUtility_RowDeselected(sender, args) {
                        var LID = args.getDataKeyValue("LID");
                        if (selected[LID]) {
                            selected[LID] = null;
                        }
                    }

                   function pageLoad() {
                       var dataItems = $find('<%=radGridWarehouseUtility.ClientID %>').get_masterTableView().get_dataItems();
                       for (var i = 0, j = dataItems.length; i < j; i++) {
                           var item = dataItems[i];
                           if (selected[item.getDataKeyValue("LID")]) {
                               item.set_selected(true);
                           }
                       }
                   }

                            <ClientSettings EnablePostBackOnRowClick="true">
                                <Selecting AllowRowSelect="True"></Selecting>
                                <ClientEvents OnRowSelected="radGridWarehouseUtility_RowSelected"
                                    OnRowDeselected="radGridWarehouseUtility_RowDeselected" />
                            </ClientSettings>

    <telerik:GridClientSelectColumn UniqueName="Select" />

    code behind...

    Protected Sub btnPrint_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrint.Click
     For Each item As GridDataItem In radGridWarehouseUtility.SelectedItems
                Dim item1 As GridDataItem = item
                Dim chkBox As CheckBox
                chkBox = DirectCast(item1.Item("Select").Controls(0), CheckBox)

                If chkBox.Enabled Then
                    Dim lid As String
                    lid = CType(radGridWarehouseUtility.MasterTableView.DataKeyValues(item.ItemIndex)("LID"), String)
                    qryParams = qryParams & lid & ","
                End If
            Next
    End Sub

    Thanks
Back to Top