I am attempting to use checkboxes for a user to select which items to delete from a DataList.
This is how I have it coded:
protected void deleteItems_Click(object sender, EventArgs e)
{
foreach(DataListItem item in pricing.Items)
{
bool IsChecked;
IsChecked = ((CheckBox)item.FindControl("Delete")).Checked;
string rowNum = ((Label)item.FindControl("lblRowNum")).Text;
if (IsChecked)
{
//delete each checked item
DB.Execute("DELETE FROM ItemWearablePricing WHERE WearablePriceingID = " + rowNum);
}
}
}
Unable to cast object of type ‘System.Web.UI.WebControls.Button’ to type ‘System.Web.UI.WebControls.CheckBox’.
I am not exactly sure what I am doing that causes this, though I am sure that someone will point out something that is obvious.
Any help that anyone can offer will be appreciated, thank you. Also, any additional code that may be needed I will do my best to put it up. Thanks to anyone that can help.
And I get the error
Presumably,
item.FindControl("Delete")is returning aButton, not aCheckBox. Double-check the names of your controls.