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