asp.net - 获取内容页面中母版页对象的 ID

标签 asp.net master-pages webforms content-pages

如果母版页有一个带有 id label1 的标签,我如何在内容页面中控制该 id。 id 没有传递下来,所以我无法控制它。例如,如果我有一个 ID 为 contentLabel 的控件,我可以通过输入 contentLabel 来访问它的代码。(无论我在做什么)

最佳答案

这里有两个选项:

1 :确保您的内容 aspx 指定 MasterType :

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

这样做可以让您的内容页面知道对母版页的期望并为您提供智能感知。因此,现在您可以继续并在母版页的代码隐藏上公开标签的 Text 属性。
public string ContentLabelText
{
    get { return contentLabel.Text; }
    set { contentLabel.Text = value; }
}

然后您可以在您的内容页面的代码隐藏页面 ala 中访问它:
Master.ContentLabelText = "hah!";

或者, 2 : 您可以通过 FindControl() 访问标签像这样:
var contentLabel = Master.FindControl("contentLabel") as Label;

关于asp.net - 获取内容页面中母版页对象的 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11145980/

相关文章:

ASP.NET MVC 3 EF CodeFirst - DBContext 项编辑

c# - 母版页中处理的异常

c# - 在代码后面的 gridview 事件处理程序中切换 DIV 可见性不起作用

c# - 如何将 "select"保留为下拉列表的 item[0]

c# - 在 Visual Studio 中使用设计 View 时隐藏母版页

c# - 按角色自定义 Sitecore 缓存

c# - 明确将链接视为绝对链接?

c# - 如何使网站状态对所有平台可用

c# - ASPX 如何通过 Windows 授权连接到另一个 IP 中的 SQL 2008?

asp.net - 在母版页上进行 jQuery Ajax 调用?