如何从内容页检索母版页中的控件
当我最近问一个有关如何从内容页检索母版页中的控件的问题时。 许多人建议我使用内容页面中的这段代码:
Label lbl = this.Master.Page.FindControl("uxLabel") as Label;
//Note any server controls defined in the master page could be not be accessible even after a cast is performed, because they could be marked as protected
这种方法当然有效,我还意识到可以使用不涉及转换 Master 属性的强类型解决方案。
在母版页位置:
public Label HeaderLabel
{
get { return uxLabel; }
}
在内容页中使用 MasterType:
<%@ MasterType VirtualPath="~/Templates/WebsiteMasterPage.master" %>
现在很容易从内容页找到控件:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.HeaderLabel.Text = "Any Text here!";
}
我想知道:
- 您对此有何看法 方法? 还有其他解决方案吗?
感谢您抽出时间
When I asked recently a question about how To retrieve a control in a Master Page from Content Page.
Many peoples suggest me to use this code from my Content Page:
Label lbl = this.Master.Page.FindControl("uxLabel") as Label;
//Note any server controls defined in the master page could be not be accessible even after a cast is performed, because they could be marked as protected
This approach certainly works, I also realize that is available a strongly-typed solution that doesn't involve casting the Master property.
In the Master Page place:
public Label HeaderLabel
{
get { return uxLabel; }
}
Using a MasterType in the Content Page:
<%@ MasterType VirtualPath="~/Templates/WebsiteMasterPage.master" %>
Now it is pretty easy find the control from the Content Page:
protected void Page_Load(object sender, EventArgs e)
{
this.Master.HeaderLabel.Text = "Any Text here!";
}
I would like to know:
- what do you think about this
approach?
any other solution?
Thanks for your time
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的回答是“为什么不呢?”。
对我来说,这两种方法都是很好的方法,但首先需要更少的编码才能开始使用,因为您不需要初始化任何类字段和设计属性。但必须在运行时找到控制。
第二种方法称为“类型化方法”,只需转换为特定的母版页类,您就可以访问任何特定于类的成员。
“类型化方法”的主要问题是什么?您需要对类库(程序集)的引用才能访问此类母版页的成员,这在某些情况下是不可取的。例如,您有一个控件库,并且想要访问强制母版页的控件,该控件提供使用某些库的控件所需的一些行为。您需要引用您的 Web 客户端程序集,但您不能这样做,因为您在 Web 客户端本身中引用您的控件库,这是一个循环引用。
工具、方法都是针对特定场景使用的。 为什么不?答案可以扩展到如果需要并且您的场景与该概念兼容,为什么不使用“类型化方法”?。
My answer is "why not?".
Both are for me good approaches but first needs less coding in order to get started with it, since you don't need to initialize any class field and design properties. But control must be found during run-time.
Second approach, call it "typed approach", is just cast to specific master page class and you get access to any class-specific member.
What would be the main problem of "typed approach"? You need a reference to the class library (assembly) in order to access to such master page's members, which wouldn't be desirable in some scenarios. For example, you've a control library and you want to access a mandatory master page's control which provides some behaviors needed to work with some library's control. You would need to reference your web client assembly, but you can't do that because you reference your control library in the web client itself, and this is a circular reference.
Tools, approaches are there for use in specific scenarios. Why not? answer can be expanded to why not to use "typed approach" if it's needed and your scenario is compatible with that concept?.