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. checkboxlist enable & disable problem

checkboxlist enable & disable problem

Scheduled Pinned Locked Moved ASP.NET
help
2 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.
  • L Offline
    L Offline
    lijukv
    wrote on last edited by
    #1

    Sub : I want the checked items to remain there when i enable and disable the checkboxlist control The no: of items remain the same but the items which where checked gets unchecked when we enable and disable the control. I saw one document in codeproject with regard to my doubt, but that document specify more in enabling and disabling the items while other items is checked or unchecked. But my requirement is about checked items when the control itself is enabled and disabled.

    M 1 Reply Last reply
    0
    • L lijukv

      Sub : I want the checked items to remain there when i enable and disable the checkboxlist control The no: of items remain the same but the items which where checked gets unchecked when we enable and disable the control. I saw one document in codeproject with regard to my doubt, but that document specify more in enabling and disabling the items while other items is checked or unchecked. But my requirement is about checked items when the control itself is enabled and disabled.

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      Hi there, The problem happens due to the fact that if a checkbox is disabled, then its value will not be submitted. In other words, the code Request.Form["myChk"] will always return null if the myChk checkbox is disabled. Let's take a look at the code implemented in the LoadPostData method

      bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
      {
      string text1 = postDataKey.Substring(this.UniqueID.Length + 1);
      int num1 = int.Parse(text1);
      if ((num1 >= 0) && (num1 < this.Items.Count))
      {
      bool flag1 = postCollection[postDataKey] != null;
      if (this.Items[num1].Selected != flag1)
      {
      this.Items[num1].Selected = flag1;
      if (!this.hasNotifiedOfChange)
      {
      this.hasNotifiedOfChange = true;
      return true;
      }
      }
      }
      return false;
      }

      When the checkbox is disabled, then the code postCollection[postDataKey] will return null, then the selected items will be reset to false, it means that they will be left 'unchecked'. So to work around this error, you are required to provide some code to persist the selected items then reset the items status after the 'Process postback data' phase in the control life cycle. You can also provide your custom checkbox list control by inheriting the standard control. Here, I follow the second option, the sample code below is a simple customized checkboxlist control:

      public class MyCheckBoxList : CheckBoxList
      {
      private object m_selectedIndices;

      protected override void LoadViewState(object savedState)
      {
      base.LoadViewState (savedState);
      //Here, I keep the selected items then it can be used later.
      m_selectedIndices = ((Triplet)savedState).Third;
      }

      protected override void OnLoad(EventArgs e)
      {
      base.OnLoad(e);
      if(!this.Enabled)
      {
      //Reset the selected item status in the Load phase.
      this.SelectInternal(m_selectedIndices as ArrayList);
      }
      }

      protected override void OnSelectedIndexChanged(EventArgs e)
      {
      //We only need to raise the event when the control is enabled.
      if(this.Enabled)
      base.OnSelectedIndexChanged (e);
      }

      internal void SelectInternal(ArrayList selectedIndices)
      {
      this.ClearSelection();
      for (int num1 = 0; num1 < selectedIndices.Count; num1

      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