如何从内容页的母版页控制 [英] How To retrieve a control in a Master Page from Content Page

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

问题描述

当我问最近一个关于如何要检索的内容页母版页中的控制问题。
许多人建议我用这个code从我的内容页:

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

此方法肯定的作品,我也认识到,可用一个强类型的解决方案,不涉及铸造Master属性。

This approach certainly works, I also realize that is available a strongly-typed solution that doesn't involve casting the Master property.

在母版页的地方:

public Label HeaderLabel
{
    get { return uxLabel; }
}

在内容页使用的MasterType:

Using a MasterType in the Content Page:

    <%@ MasterType VirtualPath="~/Templates/WebsiteMasterPage.master" %>

现在是pretty容易找到的内容页的控件:

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!";
}

我想知道:


  • 您怎么看这个
    进场?
    任何其他解决方案?

感谢您的时间

推荐答案

我的回答是为什么不呢?

两者都对我很好的方法,但首先需要更少的编码,才能开始使用它,因为你不需要初始化任何类领域和设计性能。但控制必须在运行时被发现。

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.

什么是类型办法的主要问题?你需要以类库(组装)的引用来获得这些母版页的成员,它们不会在某些情况下是可取的。例如,你已经控制库,您要访问的强制性母版页的控制提供了一些库的控制工作所需的一些行为。您将需要参考您的web客户端组件,但你不能这样做,因为你引用您的控件库在Web客户端本身,这是一个循环引用。

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?.

这篇关于如何从内容页的母版页控制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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