1

因此,我从表单中接收了一些回发数据,并且需要获取父控件中一组复选框的复选框值。我对其进行了编码,它正在工作,但现在不再工作,我不知道为什么。复选框是在页面加载时动态创建的,但在回发时,当表单已检查项目时似乎没有检查任何内容,唯一的回发事件是提交按钮事件。

                // 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>"));
                    }
                }
            }

有人可以锻炼我看不到的atm吗?

4

1 回答 1

1

视图状态没有加载到您的控件中。您必须在 LoadViewState 触发器之前创建所有控件。因此,创建所有动态控件的 OnPageInit 事件或 Page_Init 方法以获得正确的行为。查看此处以获取有关Asp.NET 页面生命周期的更多信息

希望这对你有帮助!

于 2013-05-16T13:56:42.540 回答