2013-05-16 53 views
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嗎?

+0

讓我看看你的'Page_Load'事件 – Rahul

+0

你能顯示'Page_Load'和你創建複選框代碼的方法嗎? – gzaxx

+0

我已經添加了page_load事件和複選框填充方法 – JeffreyJ

回答

1

視圖狀態沒有加載到您的控件中。您必須在LoadViewState觸發器之前創建所有控件。所以,創建所有的動態控件OnPageInit事件或Page_Init方法來獲得正確的行爲。看看這裏獲得更多的信息Asp.NET Page Life Cycle

希望這對你有所幫助!

+0

Page_Init方法做了工作,問題解決了。 謝謝你Fals! – JeffreyJ