简体   繁体   中英

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM