【问题标题】:Access Page Controls while using MasterPage使用 MasterPage 时访问页面控件
【发布时间】:2011-05-15 15:13:10
【问题描述】:

我尝试编写一个小函数来将表单重置为默认值。因此,我想访问页面的控件。我正在使用 MasterPage。也许正因为如此,我无法通过 Page.Controls 访问 ContolsCollection。

有什么解决办法吗?

【问题讨论】:

    标签: c# asp.net collections controls master-pages


    【解决方案1】:

    解决办法:

    您必须迭代所有控件并检查它们是否有自己的控件。所以你这样做递归:

    public void ResetForm(ControlCollection objSiteControls)
        {
            foreach (Control objCurrControl in objSiteControls)
            {
                string strCurrControlName = objCurrControl.GetType().Name;
    
                if (objCurrControl.HasControls())
                {
                    ResetForm(objCurrControl.Controls);
                }
    
                switch (strCurrControlName)
                {
                    case "TextBox":
                        TextBox objTextBoxControl = (TextBox)objCurrControl;
                        objTextBoxControl.Text = string.Empty;
                        break;
                    case "DropDownList":
                        DropDownList objDropDownControl = (DropDownList)objCurrControl;
                        objDropDownControl.SelectedIndex = -1;
                        break;
                    case "GridView":
                        GridView objGridViewControl = (GridView)objCurrControl;
                        objGridViewControl.SelectedIndex = -1;
                        break;
                    case "CheckBox":
                        CheckBox objCheckBoxControl = (CheckBox)objCurrControl;
                        objCheckBoxControl.Checked = false;
                        break;
                    case "CheckBoxList":
                        CheckBoxList objCheckBoxListControl = (CheckBoxList)objCurrControl;
                        objCheckBoxListControl.ClearSelection();
                        break;                    
                    case "RadioButtonList":
                        RadioButtonList objRadioButtonList = (RadioButtonList)objCurrControl;
                        objRadioButtonList.ClearSelection();
                        break;
    
                }
            }
        }
    

    【讨论】:

      【解决方案2】:

      通过使用母版页,您无法使用 FindControl() 函数访问任何控件,因为 Page 位于母版页的 contentPlaceHolder 中,因此您可以使用递归访问所有控件,例如:

         protected void Button1_Click(object sender, EventArgs e)
          {
              ReSetToDefault();
          }
      
          private void ReSetToDefault()
          {
              ResetControl(this.Page.Controls);
          }
      
          private void ResetControl(ControlCollection controlCollection)
          {
              foreach (Control con in controlCollection)
              {
                  if (con.Controls.Count > 0)
                      ResetControl(con.Controls);
                  else
                  {
                      switch (con.GetType().ToString())
                      {
                          case "System.Web.UI.WebControls.TextBox":
                              {
                                  TextBox txt = con as TextBox;
                                  txt.Text = "default value";
                              }
                              break;
                          case "System.Web.UI.WebControls.CheckBox"
                              {
                              }
                              break;
                      }
                  }
              }
          }
      

      【讨论】:

        【解决方案3】:

        母版页中的 ContentPlaceHolder 本身包含页面的所有控件, 因此,您可以使用以下方式访问它们:

        var button = ContentPlaceHolder1.FindControls("btnSubmit") as Button;
        

        请记住,代码将为继承此母版页的所有子页面运行,因此如果其中一个不包含“btnSubmit”(在上面的示例中),您将获得 null。

        【讨论】:

        • 是的,在使用它之前最好检查它是否为空。
        猜你喜欢
        • 2013-12-27
        • 1970-01-01
        • 1970-01-01
        • 2011-06-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多