Get and cast Masterpage UserControl from Content Page to access specific UC Property

前端 未结 2 1436
时光取名叫无心
时光取名叫无心 2021-01-23 06:20

I have a MasterPage (MyBoxx.Master) referencing 2 usercontrols :

<%@ Master Language=\"C#\" AutoEventWireup=\"true\" CodeFile=\"MyBoxx.master.cs\" Inherits=\"         


        
2条回答
  •  野性不改
    2021-01-23 06:27

    Well, I found several working solutions... and I think I understood how/why it worked earlier

    1) it seems that compilation has a role to play in this. If I comment the line, compile the site, and then try to add the line again, the type uxHeader is "available" in VS and I can compile the site back again with the line uncommented...

    2) As first solution is obviously not a long-term solution, I found that referencing the user-control (without actually using it of course) in the content page aspx would do the trick :

    <%@ Register TagPrefix="uc1" TagName="Header" Src="Header.ascx" %>
    

    3) I also tried this one, which I find the cleanest... In the master page, expose a public property :

    public uxHeader PageHeader
    {
        get
        {
            return Header1;//Header1 is the id of the userControl dropped in masterpage
        }
    }
    

    In the content page aspx, I then put :

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

    then, still in the content page, but in the codebehind, and after a compilation of the site, I can use :

    this.Master.PageHeader.ShowSearch = false;
    

    Hope this will help the ones searching for help on the subject in the future. I see this is a recurent question

提交回复
热议问题