在嵌套母版页中查找控件
我有一个嵌套两层的母版页。 它有一个母版页,而该母版页也有一个母版页。
当我将控件粘贴到名为“bcr”的 ContentPlaceHolder 中时 - 我必须像这样找到控件:
Label lblName =(Label)Master.Master.FindControl("bcr").FindControl("bcr").FindControl("Conditional1").FindControl("ctl03").FindControl("lblName");
我完全迷路了吗? 或者这就是需要做的事情吗?
我即将使用 MultiView,它位于条件内容控件内部。 因此,如果我想更改视图,我必须获得对该控件的引用,对吧? 获得该参考将会更加糟糕! 有没有更好的办法?
谢谢
I have a master page which is nested 2 levels. It has a master page, and that master page has a master page.
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?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
查找控件很痛苦,我一直在使用从 CodingHorror 博客 很久以前,如果传入空 id,则进行一次修改,返回 null。
在您的情况下,我认为您需要以下内容:
使用此方法通常更方便,因为您不需要确切地知道控件所在的位置以找到它(当然,假设您知道 ID),但如果您有同名的嵌套控件,您可能会得到一些奇怪的行为,因此这可能是需要注意的。
Finding controls is a pain, and I've been using this method which I got from the CodingHorror blog quite a while ago, with a single modification that returns null if an empty id is passed in.
In your case, I think you'd need the following:
Using this method is generally much more convenient, as you don't need to know exactly where the control resides to find it (assuming you know the ID, of course), though if you have nested controls with the same name, you'll probably get some strange behavior, so that might be something to watch out for.
首先,您应该知道 MasterPages 实际上位于 Pages 内部。 以至于 MasterPage 的 Load 事件实际上是在 ASPX 的 Load 事件之后调用的。
这意味着,Page 对象实际上是控件层次结构中的最高控件。
因此,了解这一点后,在此类嵌套环境中查找任何控件的最佳方法是编写一个递归函数,循环遍历每个控件和子控件,直到找到您要查找的控件。 在这种情况下,您的 MasterPage 实际上是主 Page 控件的子控件。
您可以从任何控件内部访问主 Page 对象,如下所示:
C#:
this.Page;
VB.NET
Me.Page
我发现通常情况下,Control 类的 FindControl() 方法非常无用,因为环境总是嵌套的。
因为如果是这样,我决定使用.NET 3.5 的新扩展功能来扩展 Control 类。
通过使用下面的代码 (VB.NET),在您的 AppCode 文件夹中,您的所有控件现在将通过调用 FindByControlID() 执行递归查找
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.
This means, the Page object is actually the highest control in the control hierarchy.
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.
You get to the main Page object from inside any control like this:
C#:
this.Page;
VB.NET
Me.Page
I find that usually, the Control's class FindControl() method is pretty useless, as the enviroment is always nested.
Because if this, I've decided to use .NET's 3.5 new Extension features to extend the Control class.
By using the code below (VB.NET), in say, your AppCode folder, all your controls will now peform a recursive find by calling FindByControlID()
尽管我喜欢递归,并且同意 andy 和 Mun 的观点,但您可能需要考虑的另一种方法是使用 强类型母版页。 您所要做的就是在您的 aspx 页面中添加一个指令。
不要从母版页访问页面的控件,而是考虑从页面本身访问母版页中的控件。 当您的母版页上有标题标签,并且想要从使用母版页的每个页面设置其值时,此方法非常有意义。
我不是 100% 确定,但我认为这对于嵌套母版页来说是一种更简单的技术,因为您只需将 VirtualPath 指向包含您希望访问的控件的母版即可。 但如果您想访问两个控件,每个控件各自位于一个母版页中,这可能会很棘手。
Although I love recursion, and agree with andy and Mun, one other approach you may want to consider is to have a strongly typed Master page. All you have to do is add one directive in your aspx page.
Instead of accessing a page's control from your master page, consider accessing a control in your master page from the page itself. This approach makes a lot of sense when you have a header label on your master page, and want to set its value from each page that uses the master.
I'm not 100% sure, but I think this would be simpler technique with nested master pages, as you would just point the VirtualPath to the master containing the control you wish to access. It might prove to be tricky though if you want to access two controls, one in each respective master page.
下面是一个更通用的代码,可以使用自定义条件(可以是 lambda 表达式!)
调用:
控制扩展
Here is a code that is more generic and works with a custom condition (that can be a lambda expression!)
Call:
Control extension
我使用了
<%@ MasterType VirtualPath="~/MyMaster.master" %>
方法。 我在主母版页中有一个属性,然后在详细母版页中具有相同名称的其他属性调用主主属性,并且它工作正常。我在主母版页中有这个,
这只是一个必须显示错误消息的 div 元素。 我想在带有详细信息母版页的页面中使用相同的属性(这是与主母版嵌套的)。
然后在细节主控中,我
从细节主控中调用主主控属性来创建相同的行为。
I have used the
<%@ MasterType VirtualPath="~/MyMaster.master" %>
method. I have a property in the main master page then in the detail master page other property with the same name calling the main master property and it works fine.I have this in the main master page
this is just a div element that have to show an error message. I would like to use this same property in the pages with the detail master page(this is nested with the main master).
Then in the detail master I have this
Im calling the main master property from the detail master to create the same behavior.
我刚刚让它完美运行。
在 contentpage.aspx 中,我编写了以下内容:
If Master.Master.connectsession.IsConnected then
我的编码在这里
万一
I just got it working perfectly.
In contentpage.aspx, I wrote the following:
If Master.Master.connectsession.IsConnected Then
my coded comes in here
End If