Wednesday, 22 August 2012

Dealing with dynamic checkboxlists losing their state after postback

This is probably the biggest mono oddity that we have. It could be a bug, but I am classifying it as an oddity. As far as I have found out in the MS specifications, the checkbox state of dynamic checkbox list controls is set during the prerender phase. Hence technically speaking the state of the controls is not available during Page_Load, or validation, which follows page load. While the state is not directly available in the controls, it is available in the viewstate, and can be accessed in the Request collection. We use this principle to provide a fix for lost checkboxlist states during postback. The following examples will illustrate.

(1)
Users > Users > Save
Error saving user (check log)
This error occurs because the "Sections" Checkbox list items lose their selected state.
I think this is a mono oddity. In fact, the checkbox list state is intact during
postback in Page_Load but is lost during validation, and during page rendering.
Here's the fix: it is a little clunky but it does work.
In .../umbraco/presentation/umbraco/users/EditUser.aspx.cs, modify sectionValidator_ServerValidate(...), and add after line 218,
setCheckBoxStates(lapps);

Then also add these:
        //mono fix for lost checkboxlist states
        private void setCheckBoxStates(CheckBoxList cbl)
        {
            if (IsPostBack)
            {
                string cblFormID = cbl.ClientID.Replace("_","$");
                int i = 0;
                foreach (var item in cbl.Items)
                {
                    string itemSelected = Request.Form[cblFormID + "$" + i];
                    if (itemSelected != null && itemSelected != String.Empty)
                        ((ListItem)item).Selected = true;
                    i++;
                }
            }
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            setCheckBoxStates(lapps);
        }

(2)
Settings > Document Types > Structure > Allowed child nodetypes Checkboxes state is not retained during save. This is the same issue as above. In .../umbraco/presentation/umbraco/controls/ContentTypeControlNew.aspx insert before line 285,
                        setCheckBoxStates(lstAllowedContentTypes);

and also add
        //mono fix for lost checkboxlist states
        private void setCheckBoxStates(CheckBoxList cbl)
        {
            if (IsPostBack)
            {
                string cblFormID = cbl.ClientID.Replace("_","$");
                int i = 0;
                foreach (var item in cbl.Items)
                {
                    string itemSelected = Request.Form[cblFormID + "$" + i];
                    if (itemSelected != null && itemSelected != String.Empty)
                        ((ListItem)item).Selected = true;
                    i++;
                }
            }
        }

(3)
Settings > Document Types > Info > Allowed Templates
Checkboxes state is not retained during save.
This looks like a mono oddity. When OnBubbleSave fires in
.../umbraco/presentation/umbraco/settings/EditNodeTypeNew.aspx,
the state of the checkbox is not available. We use the setCheckBoxStates(CheckBoxList cbl) fix.
In .../umbraco/presentation/umbraco/settings/EditNodeTypeNew.aspx insert before line 120,
setCheckBoxStates(templateList);

and also add
        //mono fix for lost checkboxlist states
                private void setCheckBoxStates(CheckBoxList cbl)
                {
                        if (IsPostBack)
                        {
                                string cblFormID = cbl.ClientID.Replace("_","$");
                                int i = 0;
                                foreach (var item in cbl.Items)
                                {
                                        string itemSelected = Request.Form[cblFormID + "$" + i];
                                        if (itemSelected != null && itemSelected != String.Empty)
                                                ((ListItem)item).Selected = true;
                                        i++;
                                }
                        }
                }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.