使用 MasterPage 时访问页面控件

发布于 2024-10-04 17:55:43 字数 117 浏览 3 评论 0原文

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

有什么解决办法吗?

I tried to write a little function to reset the form to default. Therefore, I want to access the Controls of the page. I'm using a MasterPage. Maybe because of that, I have no access to the ContolsCollection via Page.Controls.

Any solutions for that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

马蹄踏│碎落叶 2024-10-11 17:55:43

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

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

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

The ContentPlaceHolder within the master page itself contains all the page's controls,
So you can access them using:

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

Keep in mind that the code will be running for all child pages that inherit this master page, so if one of them does not contain "btnSubmit" (in the example above), you'll get null.

追星践月 2024-10-11 17:55:43

通过使用母版页,您无法使用 FindControl() 函数访问任何控件,因为页面位于母版页的 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;
                }
            }
        }
    }

By the use of master page you can not access any control by using FindControl() function because Page is within masterpage's contentPlaceHolder, so you can access all control by the use of Recursion like:

   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;
                }
            }
        }
    }
━╋う一瞬間旳綻放 2024-10-11 17:55:43

这里是解决方案:

您必须迭代所有控件并检查它们本身是否具有控件。所以你做这个递归:

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;

            }
        }
    }

Here the solution:

You have to iterate all controls and check is they have controls themselves. So you do this recursive:

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;

            }
        }
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文