How do you access user controls on a masterpage from the asp:content page using the master?

你。 提交于 2019-11-30 23:45:10

First add this directive to the content page you want to access the master page

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

Second, On the master page setup a public propery that returns the control you want to access

public Label MasterLabel
        {
            get
            {
                return lblMaster;
            }
            private set
            {
                //do nothing
            }
        }

Lastly just access the control in the content page like so

Master.MasterLabel.Text = "Hello from the content page!";

I know your question has been answered and this doesn't apply to it, but I noticed you're passing in 1 length characters for your "MessageCenter" control. I would use an Enum instead of a string to make your code a little less brittle. As it stands now you can pass "fart" in as a parameter and it will compile just fine. An Enum will give you some compile time checking and avoid any issues at runtime.

Examples:

Message.Success
Message.Error
Message.Warning

this is what i'd been used.

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