开发者

Accessing a master page public variable from user control

开发者 https://www.devze.com 2023-02-27 21:49 出处:网络
public partial c开发者_如何学Golass MasterPages_Main : System.Web.UI.MasterPage { public LoggedInUser ThisUser;
public partial c开发者_如何学Golass MasterPages_Main : System.Web.UI.MasterPage
{
    public LoggedInUser ThisUser;

This is in my master page, and my user control is running on the page. In my user control however, I can't seem to reference ThisUser, do I have to pass it in to the control as a parameter? Is there any way to directly reference it?


Absolutely! Take a look at the methods and properties of an asp.net page. I've done similiar using:

HtmlForm mainform = (HtmlForm)Master.FindControl("form1");

Where the important part is the Master.FindControl();

This is a part of the Page class. Page.Master will get you to the master page of the current page.


I'm sure that there's some way to avoid passing an instance of LoggedInUser to your control, however it's probably best to pass it as a parameter as doing so promotes reuse of the control because it will be more loosely coupled.

0

精彩评论

暂无评论...
验证码 换一张
取 消