This is a migrated thread and some comments may be shown as answers.

Reading Checkbox from ItemDataBound

5 Answers 149 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Scott
Top achievements
Rank 1
Scott asked on 31 Aug 2015, 11:53 PM

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

5 Answers, 1 is accepted

Sort by
0
Scott
Top achievements
Rank 1
answered on 01 Sep 2015, 06:12 PM

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)

 

 

 

0
Viktor Tachev
Telerik team
answered on 03 Sep 2015, 01:27 PM
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
0
Scott
Top achievements
Rank 1
answered on 03 Sep 2015, 04:39 PM

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>");
           }
       }
   }
‚Äč

0
Scott
Top achievements
Rank 1
answered on 03 Sep 2015, 09:45 PM

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
          }
      }
  }

0
Viktor Tachev
Telerik team
answered on 04 Sep 2015, 02:21 PM
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
Tags
Grid
Asked by
Scott
Top achievements
Rank 1
Answers by
Scott
Top achievements
Rank 1
Viktor Tachev
Telerik team
Share this question
or