469,609 Members | 1,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,609 developers. It's quick & easy.

CheckBoxList with style color

Hi,

I am populating the checkboxlist in (!IsPostBack) block of page load event
with some checked items.

After populating with checked items I am iterating through the items collection and applying color style for checked items.

The below method is called from !IsPostBack()
Expand|Select|Wrap|Line Numbers
  1.  public static void PopulateCheckBoxList(CheckBoxList checkBoxList, ArrayList nameValueList, ArrayList selectedItemsList)
  2.         {
  3.             checkBoxList.Items.Clear();
  4.             foreach (NameValueItem item in nameValueList)
  5.             {
  6.                 ListItem li = new ListItem(item.ItemName, item.ItemValue.ToLower());
  7.                 checkBoxList.Items.Add(li);
  8.             }
  9.  
  10.             foreach (NameValueItem item in selectedItemsList)
  11.             {
  12.                 checkBoxList.Items.FindByValue(item.ItemValue.ToLower()).Selected = true;
  13.                 checkBoxList.Items.FindByValue(item.ItemValue.ToLower()).Attributes.Add("class", "selectedCheckBoxListItem");
  14.             }
  15.         }
The Issue:

when the page is postedback, checkboxlist retaining its selections but losing its color style for selected items.

Please let me know why , the applied style attribute is not loaded in to viewstate.

Thanx & Regards,
only
Nov 24 '09 #1

✓ answered by onlyprad

Thanx Frinny.

I am not using AJAX.

Its fixed now.

I changed the code from:

checkBoxList.Items.FindByValue(item.ItemValue.ToLo wer()).Attributes.Add("class", "selectedCheckBoxListItem");

to:

checkBoxList.Items.FindByValue(item.ItemValue.ToLo wer()).Text = "<span class=\"selectedCheckBoxListItem\">" + checkBoxList.Items.FindByValue(item.ItemValue.ToLo wer()).Text + "</span>";



The difference in rendered HTML in the baove two is,

in the first case, <span class="selectedCheckBoxListItem"> is added outside the label containing the listitem text. And this span is not postedback.

in the second case, <span class="selectedCheckBoxListItem"> is added insode the listitem label, but around the label text value. This span is postedback and there is no style loss.

Regards,
only

3 5551
Frinavale
9,735 Expert Mod 8TB
Are you using Ajax?
Sometimes the styles aren't properly loaded after an asynchronous postback.

I'm not sure why the class wouldn't be loaded into ViewState....

-Frinny
Nov 25 '09 #2
Thanx Frinny.

I am not using AJAX.

Its fixed now.

I changed the code from:

checkBoxList.Items.FindByValue(item.ItemValue.ToLo wer()).Attributes.Add("class", "selectedCheckBoxListItem");

to:

checkBoxList.Items.FindByValue(item.ItemValue.ToLo wer()).Text = "<span class=\"selectedCheckBoxListItem\">" + checkBoxList.Items.FindByValue(item.ItemValue.ToLo wer()).Text + "</span>";



The difference in rendered HTML in the baove two is,

in the first case, <span class="selectedCheckBoxListItem"> is added outside the label containing the listitem text. And this span is not postedback.

in the second case, <span class="selectedCheckBoxListItem"> is added insode the listitem label, but around the label text value. This span is postedback and there is no style loss.

Regards,
only
Nov 25 '09 #3
@onlyprad


Thank you very much for solution.
Jan 21 '14 #4

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

reply views Thread by Bryce Budd | last post: by
3 posts views Thread by Robin Day | last post: by
4 posts views Thread by Shaul Feldman | last post: by
3 posts views Thread by Jack Black | last post: by
7 posts views Thread by bienwell | last post: by
3 posts views Thread by Sully | last post: by
reply views Thread by devrayhaan | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.