查找嵌套母版页内控制 [英] Finding controls inside nested master pages

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

问题描述

我有嵌套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?

我关于与多视图,它是一个条件内容控制的内部工作。所以,如果我想改变的观点我必须得到一个对控制权?获取该引用将是即使厉害!有没有更好的办法?

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居然坐在里面的页面。正因如此,一个母版的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.

所以,知道这一点,最好的办法找到这样一个嵌套的环境中的任何控制,是写一个递归函数来遍历每个控件和子控件,直到找到你要找的人。在这种情况下,你的MasterPages实际上是主要的页面控件的子控件。

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;

this.Page;

VB.NET

Me.Page

我发现,通常情况下,控制的类的FindControl()方法是pretty的无用的,因为环境永远嵌套。

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)的code,在说,你的应用程序code文件夹,所有控件将通过调用peform一个递归查找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天全站免登陆