find checkbox in a nested controls(Panel -> DataList)
-
I have a checkbox that is with in a datalist which is in turn inside a panel. I am trying to see if a checkbox was checked, to do some action. how can I find the checkbox? I get System.NullReferenceException: Object reference not set to an instance of an object. Thank you. the aspx.cs code protected void testb_CheckedChanged(object sender, EventArgs e) { CheckBox chkBox = (CheckBox)Page.FindControl("chkMarkerInbox"); if (chkBox.Checked == true) ..... //also tried this CheckBox chkBox = (CheckBox) pnlInbox.FindControl("chkMarkerInbox"); if (chkBox.Checked == true) .... } Here is the sample code:
-
I have a checkbox that is with in a datalist which is in turn inside a panel. I am trying to see if a checkbox was checked, to do some action. how can I find the checkbox? I get System.NullReferenceException: Object reference not set to an instance of an object. Thank you. the aspx.cs code protected void testb_CheckedChanged(object sender, EventArgs e) { CheckBox chkBox = (CheckBox)Page.FindControl("chkMarkerInbox"); if (chkBox.Checked == true) ..... //also tried this CheckBox chkBox = (CheckBox) pnlInbox.FindControl("chkMarkerInbox"); if (chkBox.Checked == true) .... } Here is the sample code:
First, you need the index of the item you need to get
CheckBox chkBox = (CheckBox) this.inboxDL.items[indexOfTheItemYouWant].findcontrol("chkMarkerInbox")
If you are getting the selectedindexchanged then:
CheckBox chkBox = (CheckBox) this.inboxDL.selectedItem.findcontrol("chkMarkerInbox")
Alexei Rodriguez
-
First, you need the index of the item you need to get
CheckBox chkBox = (CheckBox) this.inboxDL.items[indexOfTheItemYouWant].findcontrol("chkMarkerInbox")
If you are getting the selectedindexchanged then:
CheckBox chkBox = (CheckBox) this.inboxDL.selectedItem.findcontrol("chkMarkerInbox")
Alexei Rodriguez
Thanks a lot. How come with the Items it finds it and without just like: CheckBox chkBox = (CheckBox)this.inboxDL.FindControl("chkMarkerInbox"); It doesn't. Since it's a data list I have a several row of checkboxes CheckBox chkBox = (CheckBox)this.inboxDL.Items[1].FindControl("chkMarkerInbox"); When I put Items[1] it only changes state when I check that item. How do I do it when any checkbox is marked? Thank you again.
-
Thanks a lot. How come with the Items it finds it and without just like: CheckBox chkBox = (CheckBox)this.inboxDL.FindControl("chkMarkerInbox"); It doesn't. Since it's a data list I have a several row of checkboxes CheckBox chkBox = (CheckBox)this.inboxDL.Items[1].FindControl("chkMarkerInbox"); When I put Items[1] it only changes state when I check that item. How do I do it when any checkbox is marked? Thank you again.
Albert83 wrote:
How come with the Items it finds it and without just like: CheckBox chkBox = (CheckBox)this.inboxDL.FindControl("chkMarkerInbox"); It doesn't.
Because datalist has many items and wouldnt know which one you want
Albert83 wrote:
When I put Items[1] it only changes state when I check that item. How do I do it when any checkbox is marked?
Loop through all items in datalist with with a for loop datalist.items.count - 1 or for each loop
Alexei Rodriguez
-
Albert83 wrote:
How come with the Items it finds it and without just like: CheckBox chkBox = (CheckBox)this.inboxDL.FindControl("chkMarkerInbox"); It doesn't.
Because datalist has many items and wouldnt know which one you want
Albert83 wrote:
When I put Items[1] it only changes state when I check that item. How do I do it when any checkbox is marked?
Loop through all items in datalist with with a for loop datalist.items.count - 1 or for each loop
Alexei Rodriguez
Thanks a lot. With the index it goes through the checkboxes that I have and not through the datalist items. For example using your suggestion: this.inboxDL.Items[1] CheckBox chkBox = (CheckBox)this.inboxDL.Items[1].FindControl("chkMarkerInbox"); Response.Write("check"); if (chkBox.Checked == true) { ddlTest.SelectedItem.Text = "works"; } Only when I click on checkbox with index[1] ddlTest will change to "works". But I have 5 more checkboxes which are not changing the state of ddlTest to "works". So it goes through the checkboxes and not the datalist items as I understand. Is it so? But I made it work as follows. This way I assume when it encounter non checkbox controls it does nothing and when it does with Findcontrol it stores true/false in the isDeleted variable. That's how I think the code works. bool isDeleted = false; foreach (DataListItem anItem in inboxDL.Items) { isDeleted = ((CheckBox)anItem.FindControl("chkMarkerInbox")).Checked; if (isDeleted) { lbDel.Enabled = true; ddlTest.SelectedItem.Text = "works"; } } Thanks again.