无法访问 UpdatePanel 内的控件

发布于 2024-08-15 09:48:22 字数 1152 浏览 6 评论 0原文

我想做的是在 Page_Load 访问页面控件,并进行数据库查询,并使控件可见或不可见。

代码如下:

foreach (Control thiscontrol in ContentPlaceHolderBody.Controls) {
    try {
        if (thiscontrol.ID.Contains("TextBox") || thiscontrol.ID.Contains("Label")) {
            string dummy = thiscontrol.ID;
            bool IsValid = db.Roles.Any(a => a.controlName == dummy);
            if (IsValid == false)
                thiscontrol.Visible = false;
        }
        else if (thiscontrol.ID.Contains("UpdatePanel")) {
            foreach (Control UPcontrols in ((UpdatePanel)thiscontrol).ContentTemplateContainer.Controls) {
                if (UPcontrols.ID.Contains("TextBox") || UPcontrols.ID.Contains("DropDownList")) {
                    bool UPIsValid = db.Roles.Any(a => a.controlName == UPcontrols.ID);
                    if (UPIsValid == false)
                        UPcontrols.Visible = false;
                }
            }
        }
    }
    catch { }
}

我的问题出在 UPcontrols 上!它应该检索 UpdatePanel 中的控件,但问题是它不执行其工作,除非在调试模式下!

当我添加断点时,一切正常,但是当我运行 Web 应用程序时,它在 UpdatePanel 中找不到任何组件...

What I am trying to do is accessing Page Controls at Page_Load, and make a database query, and make controls visible or not visible.

Here is the Code:

foreach (Control thiscontrol in ContentPlaceHolderBody.Controls) {
    try {
        if (thiscontrol.ID.Contains("TextBox") || thiscontrol.ID.Contains("Label")) {
            string dummy = thiscontrol.ID;
            bool IsValid = db.Roles.Any(a => a.controlName == dummy);
            if (IsValid == false)
                thiscontrol.Visible = false;
        }
        else if (thiscontrol.ID.Contains("UpdatePanel")) {
            foreach (Control UPcontrols in ((UpdatePanel)thiscontrol).ContentTemplateContainer.Controls) {
                if (UPcontrols.ID.Contains("TextBox") || UPcontrols.ID.Contains("DropDownList")) {
                    bool UPIsValid = db.Roles.Any(a => a.controlName == UPcontrols.ID);
                    if (UPIsValid == false)
                        UPcontrols.Visible = false;
                }
            }
        }
    }
    catch { }
}

My Problem is with the UPcontrols! It should retrieve the controls within the UpdatePanel, but the thing is it doesn't do its job, except in the debug mode!

When I add a breakpoint, everything is OK, but when I run the web application, it doesn't find any components within the UpdatePanel...

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

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

发布评论

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

评论(2

山有枢 2024-08-22 09:48:22

试试这个:

ControlCollection cbb = updatepanel1.Controls;
ControlCollection cb = cbb[0].Controls;

initialize_Controls(cb);

public void initialize_Controls(ControlCollection objcontrls)
{

    foreach (Control tb in objcontrls) {
        if (tb is TextBox)
            ((TextBox)tb).Text = "";


        if (tb is Panel) {
            ControlCollection cbcll = tb.Controls;

            foreach (Control tbb in cbcll) {
                if (tbb is TextBox)
                    ((TextBox)tbb).Text = "";
            }
        }
    }
}

首先从 updatepanel 中查找控件,即 ContentTemplate,然后从 contentTemplate 中查找包含其中所有控件的控件。

Try this one:

ControlCollection cbb = updatepanel1.Controls;
ControlCollection cb = cbb[0].Controls;

initialize_Controls(cb);

public void initialize_Controls(ControlCollection objcontrls)
{

    foreach (Control tb in objcontrls) {
        if (tb is TextBox)
            ((TextBox)tb).Text = "";


        if (tb is Panel) {
            ControlCollection cbcll = tb.Controls;

            foreach (Control tbb in cbcll) {
                if (tbb is TextBox)
                    ((TextBox)tbb).Text = "";
            }
        }
    }
}

First find controls from updatepanel i.e ContentTemplate, then find controls from contentTemplate which contain all controls in it.

青丝拂面 2024-08-22 09:48:22

这看起来是一个非常奇怪的设计。也就是说,将控件 ID 用于此类目的是相当不寻常的。

尽管如此,您在这里需要一个递归方法来深入遍历页面上的每个控件。如果 UpdatePanel 包含在另一个控件中,您的方法将不起作用。

This seems like a very bizarre design. That is, using control IDs for such purposes is rather unusual.

Nevertheless, you need a recursive method here to do a deep walk of every control on the page. Your method will not work if the UpdatePanel is contained within another control.

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