0

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.

3
  • 1
    Maybe a similar issue to this Commented Apr 7, 2017 at 6:15
  • @john I edited the question with a way that didn't work. It is quite similar. Commented Apr 7, 2017 at 6:19
  • Please show us the html you have Commented Apr 7, 2017 at 6:22

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

5 Comments

Hi Adil, thanks for the answer. However, I tried e.Item.FindControl("lblEditCheck") and it returns null. There is a checkbox for sure as I can see it in the markup. Any other way that might work. Thanks.
It should work, did you use FindControl under condition, if (e.Item.ItemType == ListViewItemType.DataItem)? Also add complete related HTML.
I can't do that as it says Operator == can't be applied to items of type ListViewItem.
also one silly question: As a result of this exception in my code: The application has a bug that : loads only one row of a data grid. I think, why even one row is loaded correctly.
It should not give you operator error, did you use ListViewItemType.DataItem? Show me your code on which you get error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.