CheckBoxList with style color

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • onlyprad
    New Member
    • Nov 2009
    • 4

    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()
    Code:
     public static void PopulateCheckBoxList(CheckBoxList checkBoxList, ArrayList nameValueList, ArrayList selectedItemsList)
            {
                checkBoxList.Items.Clear();
                foreach (NameValueItem item in nameValueList)
                {
                    ListItem li = new ListItem(item.ItemName, item.ItemValue.ToLower());
                    checkBoxList.Items.Add(li);
                }
    
                foreach (NameValueItem item in selectedItemsList)
                {
                    checkBoxList.Items.FindByValue(item.ItemValue.ToLower()).Selected = true;
                    checkBoxList.Items.FindByValue(item.ItemValue.ToLower()).Attributes.Add("class", "selectedCheckBoxListItem");
                }
            }
    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
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    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

    Comment

    • onlyprad
      New Member
      • Nov 2009
      • 4

      #3
      Thanx Frinny.

      I am not using AJAX.

      Its fixed now.

      I changed the code from:

      checkBoxList.It ems.FindByValue (item.ItemValue .ToLower()).Att ributes.Add("cl ass", "selectedCheckB oxListItem");

      to:

      checkBoxList.It ems.FindByValue (item.ItemValue .ToLower()).Tex t = "<span class=\"selecte dCheckBoxListIt em\">" + checkBoxList.It ems.FindByValue (item.ItemValue .ToLower()).Tex t + "</span>";



      The difference in rendered HTML in the baove two is,

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

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

      Regards,
      only

      Comment

      • Aikanaro
        New Member
        • Jan 2014
        • 1

        #4
        Originally posted by onlyprad
        Thanx Frinny.

        I am not using AJAX.

        Its fixed now.

        I changed the code from:

        checkBoxList.It ems.FindByValue (item.ItemValue .ToLower()).Att ributes.Add("cl ass", "selectedCheckB oxListItem");

        to:

        checkBoxList.It ems.FindByValue (item.ItemValue .ToLower()).Tex t = "<span class=\"selecte dCheckBoxListIt em\">" + checkBoxList.It ems.FindByValue (item.ItemValue .ToLower()).Tex t + "</span>";



        The difference in rendered HTML in the baove two is,

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

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

        Regards,
        only


        Thank you very much for solution.

        Comment

        Working...