Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. find checkbox in a nested controls(Panel -> DataList)

find checkbox in a nested controls(Panel -> DataList)

Scheduled Pinned Locked Moved ASP.NET
question
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Albert83
    wrote on last edited by
    #1

    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:

    A 1 Reply Last reply
    0
    • A Albert83

      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:

      A Offline
      A Offline
      AlexeiXX3
      wrote on last edited by
      #2

      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

      A 1 Reply Last reply
      0
      • A AlexeiXX3

        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

        A Offline
        A Offline
        Albert83
        wrote on last edited by
        #3

        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.

        A 1 Reply Last reply
        0
        • A Albert83

          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.

          A Offline
          A Offline
          AlexeiXX3
          wrote on last edited by
          #4

          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

          A 1 Reply Last reply
          0
          • A AlexeiXX3

            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

            A Offline
            A Offline
            Albert83
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups