Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
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;
                   break;
               }
           }
Posted
Updated 18-Mar-15 2:12am
v2

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

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

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


The casting will depend on how have you declared it in your aspx
 
Share this answer
 
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

C#
// 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.
 
Share this answer
 

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