ASP.NET 如何访问父页面上深层嵌套的用户控件

发布于 2024-09-19 17:22:50 字数 652 浏览 4 评论 0原文

我有一个登录控件,并且 at 在标头控件中嵌套了 2 层 即页面-->标头控制 -->登录控制。我无法使用 FindControl 获取对页面上控件的引用。我希望能够设置控件的可见属性,就像

  if (_loginControl != null)
            _loginControl.Visible = false;

我最终使用递归 FindControl 方法来查找嵌套控件一样。

    public static Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }

        return null;
    }

I have a login control and at is nested 2 deep in a header control
i.e Page --> Header Control --> Login Control. I cannot get a reference to the control on the page using FindControl. I want to be able to set the visible property of the control like

  if (_loginControl != null)
            _loginControl.Visible = false;

I ended up using a recursive FindControl method to find the nested control.

    public static Control FindControlRecursive(Control root, string id)
    {
        if (root.ID == id)
        {
            return root;
        }

        foreach (Control c in root.Controls)
        {
            Control t = FindControlRecursive(c, id);
            if (t != null)
            {
                return t;
            }
        }

        return null;
    }

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

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

发布评论

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

评论(5

聊慰 2024-09-26 17:23:12
private List<Control> GetAllNestedUserControl(Control ph)
    {
        List<Control> Get = new List<Control>();
        foreach (var control in ph.Controls)
        {
            if (control is UserControl)
            {
                UserControl uc = control as UserControl;
                if (uc.HasControls())
                {
                   Get =  GetAllNestedUserControl(uc);

                }
            }
            else
            {
                Control c = (Control)control;
                if (!(control is LiteralControl))
                {
                     Get.Add(c);
                }
            }
        }
        return Get;
    }

只需从任何父页面调用此代码,然后通过以下代码获取任何控制

        List<Control> Get = GetAllNestedUserControl(ph);
        Label l = (Label)Get.Find(o => o.ID == "lblusername");
        l.Text = "changed from master";
private List<Control> GetAllNestedUserControl(Control ph)
    {
        List<Control> Get = new List<Control>();
        foreach (var control in ph.Controls)
        {
            if (control is UserControl)
            {
                UserControl uc = control as UserControl;
                if (uc.HasControls())
                {
                   Get =  GetAllNestedUserControl(uc);

                }
            }
            else
            {
                Control c = (Control)control;
                if (!(control is LiteralControl))
                {
                     Get.Add(c);
                }
            }
        }
        return Get;
    }

just call this code from you any parent page and then get any control by the following code

        List<Control> Get = GetAllNestedUserControl(ph);
        Label l = (Label)Get.Find(o => o.ID == "lblusername");
        l.Text = "changed from master";
故事与诗 2024-09-26 17:23:09

登录控件(如果在标记中注册)也将成为代码隐藏页面的实例成员;您可以从代码隐藏类中引用它,就好像它是普通成员一样,使用与您提供的 ID 相同的名称(顺便说一句,我建议对大多数逻辑使用代码隐藏,而不是在标记中内联代码)。

您还可以使用页面的 FindControl() 方法,该方法将在其控件子树中搜索具有给定 ID 的控件。这需要更长的时间,所以我会推荐第一个选项,除非逻辑控制是动态添加的并且您并不总是知道它在那里。

The login control, if it's registered in the markup, will also be an instance member of your codebehind page; you can refer to it from the codebehind class as if it were a normal member, using the same name you provided as the ID (I do recommend using codebehinds for most logic, instead of inlining code in the markup, BTW).

You can also use the FindControl() method of your page, which will search its control subtree for a control with a given ID. That takes longer, so I would recommend the first option unless the logic control is added dynamically and you don't always know it's there.

你怎么敢 2024-09-26 17:23:06

尝试调用 this.FindControl("_loginControl") 或 this.Page.FindControl("_loginControl")。

方法详情参见MSDN:
http://msdn.microsoft.com/ en-us/library/system.web.ui.control.findcontrol.aspx

Try calling this.FindControl("_loginControl") or this.Page.FindControl("_loginControl").

See MSDN for method details:
http://msdn.microsoft.com/en-us/library/system.web.ui.control.findcontrol.aspx

天煞孤星 2024-09-26 17:23:03

一个好的方法是使用:

Page.FindControl() 

如果产生 null,则控件不存在。

A good way would be to use:

Page.FindControl() 

if that yields null, the control is not there.

追风人 2024-09-26 17:23:00

您是否需要从用户控件所在的 ASP.NET 页面禁用/隐藏用户控件(或者用户控件是否存在于母版页上)?如果它位于同一页面中,则在 ASP.NET 页面的代码隐藏中执行以下操作:

MyUserControlsID.Visible = false

其中 MyUserControl 是用户控件的 ID。要确定用户控件的 ID,请查看 .aspx 页面的标记,您将看到类似这样的内容:

<uc1:UserControlName ID="MyUserControlsID" runat="server" ... />

快乐编程!

Are you needing to disable/hide the User Control from the ASP.NET page it resides on (or does the User Control exist on a master page, say)? If it's in the same page, then in your ASP.NET page's code-behind you'd do:

MyUserControlsID.Visible = false

Where MyUserControl is the ID of your User Control. To determine the ID of your User Control look at the markup of your .aspx page and you will see something like this:

<uc1:UserControlName ID="MyUserControlsID" runat="server" ... />

Happy Programming!

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