【问题标题】:Checkbox group not detecting if checked复选框组未检测是否选中
【发布时间】:2013-05-16 13:45:44
【问题描述】:

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

                // 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 看不到的东西吗?

【问题讨论】:

  • 告诉我你的Page_Load活动
  • 你能显示Page_Load 和你创建复选框代码的方法吗?
  • 我已经添加了 page_load 事件和复选框填充方法

标签: c# asp.net postback web-controls


【解决方案1】:

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

希望对您有所帮助!

【讨论】:

  • Page_Init 方法完成了工作,问题解决了。谢谢法尔斯!
猜你喜欢
  • 2018-02-09
  • 2022-01-12
  • 1970-01-01
  • 1970-01-01
  • 2019-07-04
  • 2015-04-10
  • 1970-01-01
  • 2016-04-22
  • 1970-01-01
相关资源
最近更新 更多