Question

So I am recieving some postback data back from a form and need to get the checkbox values for a group of checkboxes in a parent control. I coded it up and it was working but now no longer works and I can not figure out why. The checkboxes are created on page load dynamically but on postback nothing seems to be checked when the form had checked items, the only postback event is the submit button event.

                // This is from the btnSubmit Postback event that isn't working anymore
                foreach (CheckBox cb in ShowPermissions.Controls.OfType<CheckBox>())
                {
                    if (cb.Checked)
                    {
                        // Add New Admins Permissions
                        Permission p = new Permission();
                        p.AdminUserID = au.id;
                        p.AdminMenuID = Convert.ToInt32(cb.ID.ToString().Substring(4));
                        ngdb.Permissions.InsertOnSubmit(p);
                        submitResult.InnerHtml += cb.ID.ToString();
                        // Does not run now?
                    }
                    // can see the checkbox object
                }


                protected void Page_Load(object sender, EventArgs e)
            {
                FunctionType = Request.QueryString["func"] != null && Request.QueryString["func"] != "" ? Request.QueryString["func"] : null;
                RID = Request.QueryString["rid"] != null && Request.QueryString["rid"] != "" ? int.Parse(Request.QueryString["rid"]) : -1;

                PopulateAdminTypes();

                if (!IsPostBack && FunctionType == "edit" && RID != -1)
                {
                    // Populate User details for Edit
                    PopulateUser(RID);
                    // Populate checkboxes and check selected options
                    PopulateAdminPermissionOptions(true, RID);
                    // Disable password change
                    ChangePassword(false);
                }
                else if (!IsPostBack)
                {
                    chkChangePassword.Visible = false;
                    PopulateAdminPermissionOptions(false, -1);
                }

            }

                private void PopulateAdminPermissionOptions(bool blnPopulateForEdit, int RID)
            {
                // Get Logged in Admin ID
                int intAdminId = Convert.ToInt32(Session["AdminID"]);
                int intAdminTypeId = Convert.ToInt32(Session["AdminTypeID"]);

                using (NewGeorgeDataContext ngdb = new NewGeorgeDataContext())
                {
                    var am = ngdb.AdminMenus.AsQueryable();
                    // Hide Add and Edit Options from Non Super Users
                    var amUsers = ngdb.AdminMenus.Where(x => x.id > 2 && x.id < 5);
                    if (intAdminTypeId > 1) am = am.Except(amUsers);

                    foreach (var m in am.OrderBy(x => x.MenuTypeID).ThenBy(x => x.id))
                    {
                        // Add New CheckBox
                        CheckBox cb = new CheckBox();
                        cb.ID = "chk_" + m.id;
                        cb.CssClass = "chkItems";
                        cb.Text = m.AdminMenuType.Name + ": " + m.Name;
                        // Get Admin Permission objects
                        if (blnPopulateForEdit)
                        {
                            var ap = ngdb.Permissions.SingleOrDefault(x => x.AdminUserID == RID && x.AdminMenuID == m.id);
                            if (ap != null)
                            {
                                cb.Checked = true;
                            }
                        }
                        ShowPermissions.Controls.Add(new LiteralControl("<p>"));
                        ShowPermissions.Controls.Add(cb);
                        ShowPermissions.Controls.Add(new LiteralControl("</p>"));
                    }
                }
            }

Can someone workout what I cannot see atm?

Was it helpful?

Solution

The view state isn't getting load into your controls. You must create all the controls before LoadViewState triggers. So, create all dynamic Controls OnPageInit event or Page_Init method to get the correct behavior. Take a look here to get more information about Asp.NET Page Life Cycle

Hopes this help you!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top