在嵌套母版页中查找控件 [英] Finding controls inside nested master pages

查看:26
本文介绍了在嵌套母版页中查找控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个嵌套 2 层的母版页.它有一个母版页,该母版页有一个母版页.

I have a master page which is nested 2 levels. It has a master page, and that master page has a master page.

当我将控件粘贴到名为bcr"的 ContentPlaceHolder 中时 - 我必须像这样找到控件:

When I stick controls in a ContentPlaceHolder with the name "bcr" - I have to find the controls like so:

 Label lblName =(Label)Master.Master.FindControl("bcr").FindControl("bcr").FindControl("Conditional1").FindControl("ctl03").FindControl("lblName");

我完全迷路了吗?或者这就是它需要完成的方式?

Am I totally lost? Or is this how it needs to be done?

我即将使用 MultiView,它位于条件内容控件内.因此,如果我想更改视图,我必须获得对该控件的引用,对吗?获得该参考将更加令人讨厌!有没有更好的办法?

I am about to work with a MultiView, which is inside of a conditional content control. So if I want to change the view I have to get a reference to that control right? Getting that reference is going to be even nastier! Is there a better way?

谢谢

推荐答案

首先,您应该知道 MasterPages 实际上位于 Pages 中.以至于 MasterPage 的 Load 事件实际上是在 ASPX 的 Load 事件之后调用的.

Firstly, you should know that MasterPages actually sit inside Pages. So much so that a MasterPage's Load event is actually called after your ASPX's Load event.

这意味着,Page 对象实际上是控件层次结构中的最高控件.

This means, the Page object is actually the highest control in the control hierarchy.

因此,知道这一点,在这种嵌套环境中找到任何控件的最佳方法是编写一个递归函数,循环遍历每个控件和子控件,直到找到您要查找的控件.在这种情况下,您的 MasterPage 实际上是主 Page 控件的子控件.

So, knowing this, the best way to find any control in such a nested environment, is to write a recursive function that loops through every control and child controls until it finds the one you're looking for. in this case, your MasterPages are actually child controls of the main Page control.

您可以像这样从任何控件内部访问主 Page 对象:

You get to the main Page object from inside any control like this:

C#:

this.Page;

VB.NET

我.Page

我发现通常情况下,控件的类 FindControl() 方法非常无用,因为环境总是嵌套的.

I find that usually, the Control's class FindControl() method is pretty useless, as the enviroment is always nested.

因为如果是这样,我决定使用 .NET 的 3.5 新扩展特性来扩展 Control 类.

Because if this, I've decided to use .NET's 3.5 new Extension features to extend the Control class.

通过使用下面的代码 (VB.NET),比如你的 AppCode 文件夹,你的所有控件现在将通过调用 FindByControlID() 来执行递归查找

By using the code below (VB.NET), in say, your AppCode folder, all your controls will now peform a recursive find by calling FindByControlID()

    Public Module ControlExtensions
    <System.Runtime.CompilerServices.Extension()> _
    Public Function FindControlByID(ByRef SourceControl As Control, ByRef ControlID As String) As Control
        If Not String.IsNullOrEmpty(ControlID) Then
            Return FindControlHelper(Of Control)(SourceControl.Controls, ControlID)
        Else
            Return Nothing
        End If
    End Function

    Private Function FindControlHelper(Of GenericControlType)(ByVal ConCol As ControlCollection, ByRef ControlID As String) As Control
        Dim RetControl As Control

        For Each Con As Control In ConCol
            If ControlID IsNot Nothing Then
                If Con.ID = ControlID Then
                    Return Con
                End If
            Else
                If TypeOf Con Is GenericControlType Then
                    Return Con
                End If
            End If

            If Con.HasControls Then
                If ControlID IsNot Nothing Then
                    RetControl = FindControlByID(Con, ControlID)
                Else
                    RetControl = FindControlByType(Of GenericControlType)(Con)
                End If

                If RetControl IsNot Nothing Then
                    Return RetControl
                End If
            End If
        Next

        Return Nothing
    End Function

End Module

这篇关于在嵌套母版页中查找控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆