Rafael
Rafael

Reputation: 671

Exception in ASP.NET WEB Application

I have a function in an application:

private void ds_ItemBound(object sender, DataGridItemEventArgs e)

where in this line:

((System.Web.UI.WebControls.CheckBox)(e.Item.Cells[3].Controls[0])).Checked = false;

I get an exception:

Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.CheckBox'.

How do I solve this casting error?

Thanks for your help.

Upvotes: 0

Views: 70

Answers (2)

Reddog
Reddog

Reputation: 15579

It would seem that your first control in the forth column is not a checkbox. I would advise using the FindControl function instead of your args' Item property.

private void ds_ItemBound(object sender, DataGridItemEventArgs e)
{
    if (e.Item.ItemType != ListItemType.Item && e.Item.ItemType != ListItemType.AlternatingItem) return;

    var checkbox = (CheckBox) e.Item.FindControl("lblEditCheck");
    checkbox.Checked = false;
}

Upvotes: 2

borkovski
borkovski

Reputation: 978

Make sure your CheckBox is first in the fourth cell, it looks like there's a Literal. Please post fragment of your page code to confirm.

Upvotes: 0

Related Questions