Get ID of Master Page object in Content Page

安稳与你 提交于 2019-12-23 09:46:34

问题


If the master page has a label with the id label1 how do I control that id in the content page. The id is not passed down so i can't control it inherently. For example if i have a control with the id contentLabel i can access it code by just typing contentLabel.(whatever i'm doing)


回答1:


Here are two options:

1: make sure your content aspx specifies MasterType:    

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

Doing this lets your content page know what to expect from your master-page and gives you intellisense. So, now you can go ahead and expose the label's Text property on the master page's code-behind.

public string ContentLabelText
{
    get { return contentLabel.Text; }
    set { contentLabel.Text = value; }
}

Then you can access it in your content page's code-behind page ala:

Master.ContentLabelText = "hah!";

or, 2: You can access the label via FindControl() like so:

var contentLabel = Master.FindControl("contentLabel") as Label;


来源:https://stackoverflow.com/questions/11145980/get-id-of-master-page-object-in-content-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!