Rafael
Rafael

Reputation: 671

Casting Exception in ASP.NET Web Application

I have a function:

  private void ds_ItemBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemIndex >= 0)
            {
                e.Item.Cells[0].Controls.Add(Utility.GetImage("Delegate"));

                e.Item.Attributes.Add("Description", ((System.Data.DataRowView)(e.Item.DataItem)).Row["DelegateDescription"].ToString());
                string DelegateName = ((System.Data.DataRowView)(e.Item.DataItem)).Row["DelegateName"].ToString();

                e.Item.Attributes.Add("DelegateName", ((System.Data.DataRowView)(e.Item.DataItem)).Row["DelegateName"].ToString());

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


                if (hDelegates.Value != "")
                {

                    string[] selectedDelegates = hDelegates.Value.Split(new char[] { ',' });
                    foreach (string delegate in selectedDelegates)
                    {

                        if (delegate.Equals(delegateName))
                        {
                            count++;
                            ((System.Web.UI.WebControls.CheckBox)(e.Item.Cells[3].Controls[0])).Checked = true;

                            break;
                        }
                    }
                }
            }
            if (count == dDelegates.Items.Count)
                lblEditCheck.Checked = true;
            else
                lblEditCheck.Checked = false;
        }

The UI has a table with each row corresponding to one entry. So, each row has an image, a description, a name.

I am getting an exception in the code at line:

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

The exception is : Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'System.Web.UI.WebControls.CheckBox'.

Further, e.Item.Cells[3].Controls[0] is System.Web.UI.LiteralControl .

And the the check box markup looks like <asp:CheckBox ID="lblEditCheck" runat="server" TabIndex="0" Width="30px" CssClass="ColumnHeader" /></td>

I am stuck on getting this to work for a few days now. Please let me know if any more info will be useful.

Thanks a lot for your help.

What didn't work:

var checkbox = (System.Web.UI.WebControls.CheckBox)e.Item.FindControl("lblEditCheck");
checkbox.Checked = false;

In this case checkbox is null. And the corresponding exception is thrown.

Upvotes: 0

Views: 132

Answers (1)

Adil
Adil

Reputation: 148120

Use IDs of the controls which you want to get and use the FindControl to get the CheckBox. Also use the ListViewItemType.DataItem for data rows as you will get the null for non DataItem rows.

if (e.Item.ItemType == ListViewItemType.DataItem)
{
    ((System.Web.UI.WebControls.CheckBox)(e.Item.FindControl("lblEditCheck")).Checked = false;
}

If you expect the some of row may not have the checkbox control then you can first get the checkbox and then change it Changed status.

CheckBox lblEditCheck = e.Item.FindControl("lblEditCheck") as CheckBox;
if(lblEditCheck != null)
     lblEditCheck.Checked = false;

Upvotes: 1

Related Questions