Click here to Skip to main content
15,034,292 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
bool isSelected = false;
           foreach(GridViewRow i in DataGridTrans.Rows)
               CheckBox cb = new CheckBox();

         cb = (CheckBox)i.FindControl("ChkBox1");//error showing cast as mentioned above
               if (cb != null && cb.Checked)
                   isSelected = true;
Updated 18-Mar-15 2:12am

If you look at your markup you will see that the checkbox defined as
<input type="checkbox" />

and not as
<asp:checkbox xmlns:asp="#unknown" />

Obviously you ca nnot convert between the two -you have to decide what to use and stick to that...
Try it like:
var cb = (HtmlInputCheckBox)row.FindControl("ChkBox1");

The casting will depend on how have you declared it in your aspx
Your "ChkBox1" is probably

<input type="checkbox" runat="server"/>

which gets converted to HtmlInputCheckBox. The alternative is

<asp:Checkbox runat="server"/>

which gets converted to Checkbox. If you want to keep your markup as it is you need to use the HtmlInputCheckBox in your code-behind, not Checkbox

// don't need this line
// CheckBox cb = new CheckBox();

HtmlInputCheckBox cb = (HtmlInputCheckBox)i.FindControl("ChkBox1");//error showing cast as mentioned above
 if (cb != null && cb.Checked)

To use this you need

using System.Web.UI.HtmlControls;

at the top of your page.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900