Friday, November 25, 2011

Check or uncheck items in a checkbox list, based on information

This example shows you the way to get the value of checked items and then again check the checkbox a/c to the values.
 foreach (ListItem val in ChklPeriod.Items)
 {
      if (val.Selected)
            ViewState["data"] += val.Text + ",";  //store the selected values in viewstate separated by a comma
 }

Now to check the checkboxlist a/c to the stored values use the method given below.
if (ViewState["data"] != null)
{
      foreach (string s in ViewState["data"].ToString().Split(new Char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
      {
             foreach (ListItem l in ChklPeriod.Items)
                        if (l.Text == s)
                            l.Selected = true;
      }
}

No comments:

Post a Comment