user2575502 user2575502 - 4 years ago 124
ASP.NET (C#) Question

Checked property is not accessible in asp.net

I want to access "Checked" property against a checkbox control in GridView, please see my aspx page code below firstly:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="cbDeleteAll" runat="server" OnCheckedChanged="cbDeleteAll_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbDelete" runat="server" OnCheckedChanged="cbDelete_CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Employee ID">
<ItemTemplate>
<asp:Label ID="lblEmpId" runat="server" Text='<%# Bind("DeptID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmployeeName" HeaderText="Employee Name" />
<asp:BoundField DataField="DeptID" HeaderText="Department" />
</Columns>
</asp:GridView>


in the "cbDeleteAll_CheckedChanged" event, I would like to see the Checked property on the header template and set value to Checked property to checkboxes in the item template, see below:

enter image description here

as you can see, there is a red wave line under "Checked" property, when I rebuild the solution, I can see the error message as below:

enter image description here

so, I was totally confused I don't know why I cannot access this property in the code?
can anyone help me or give me some instructions?

Answer Source

Look at the error. It says WebApplication1.CheckBox. It appears you have a CheckBox class in your app and therefore you're casting to the wrong type. Don't name your classes the same thing as something built into the framework.

To find the offending file put your cursor over CheckBox in your class and press Function 12 or right click it and select Go To Definition. Then right click that class and rename it something more appropriate.

The correct CheckBox class is in System.Web.UI.WebControls namespace.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download