Reading Checkbox from ItemDataBound

6 posts, 0 answers
  1. Scott
    Scott avatar
    22 posts
    Member since:
    Aug 2012

    Posted 31 Aug 2015 Link to this post

    I have a RadGrid that is bound to a SQL database.  On the select statement I generate a column that is not in the table.  "SELECT '-' as [CheckBox] ...".  I then use the ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e) event on the RadGrid to add a column of check boxes (one on each row).  The user selects certain check boxes (specifying a row) and presses a submit button.  How can I read what rows were selected?  

    I do not use the information to update the database.  The database is used for management and does not change.  If I can't read the selection is there a different way to do this?  Using a RadGrid is just so nice so I would like to keep using that, but I don't need to bind it to a database.  

     

    Thanks,
     Scott

  2. Scott
    Scott avatar
    22 posts
    Member since:
    Aug 2012

    Posted 01 Sep 2015 in reply to Scott Link to this post

    It seems like there is a much easier way to do this.  I followed the example at the end of this thread, but the button click event does not indicate that anything was selected.  I am on an old version and the post was about a change from v2009 to v2012.  Could my problem be a version problem? 

    Row Selection Example

    (http://www.telerik.com/forums/can-t-get-selected-items-on-server-side-in-a-radgrid-with-checkbox)

     

     

     

  3. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2213 posts

    Posted 03 Sep 2015 Link to this post

    Hello Scott,

    Please examine the following article that describe in detail how you can implement server-side selection for items in RadGrid via CheckBox.




    Regards,
    Viktor Tachev
    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
  4. Scott
    Scott avatar
    22 posts
    Member since:
    Aug 2012

    Posted 03 Sep 2015 in reply to Viktor Tachev Link to this post

    Thank you for that reference.  I have looked at it, but I can't see how to read all the selected items on a submit button click.  The example and what it referenced did not seem to do that.  

    I have tried the code below, but it fails on "CheckBox chk = (CheckBox)item["CheckBox1"].Controls[0];".  If I try "(dataItem.FindControl("CheckBox1") as CheckBox).Checked" that seems to select everything.  The goal is to be able to identify which rows were selected and give me the value of one item in that row.    

     

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

  5. Scott
    Scott avatar
    22 posts
    Member since:
    Aug 2012

    Posted 03 Sep 2015 in reply to Scott Link to this post

    The fix was too easy.  Just use item.selected. 

     

    protected void Button1_Click(object sender, EventArgs e)
      {
          foreach (GridDataItem item in WebServerRadGrid.MasterTableView.Items)
          {
              if (item.Selected == true)
              {
                 // Do something
              }
          }
      }

  6. Viktor Tachev
    Admin
    Viktor Tachev avatar
    2213 posts

    Posted 04 Sep 2015 Link to this post

    Hello Scott,

    I am glad that you have the functionality working.

    Thank you for sharing your approach with the community. This can be helpful for someone that is trying to implement similar functionality.

    Regards,
    Viktor Tachev
    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
Back to Top