簡體   English   中英

在C#中使用母版頁和Web表單

[英]Using master page and web forms in C#

當使用母版頁時,無論如何我可以看到當前正在加載或正在母版頁中加載的Web表單,因此在此行之前:

<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

</asp:ContentPlaceHolder>

我想查看/獲取要在母版頁中加載的Web表單,這可能嗎?

是的,您可以從母版頁訪問Page屬性。

若要查看實際效果,請從ASP.NET Web應用程序的默認Visual Studio模板(包含母版頁)中將其轉儲到母版中,其中h1標題為:

My ASP.NET Application <%= Page.GetType().Name %>

它將在瀏覽器中顯示為“ My ASP.NET Application default_aspx或您所在的任何頁面。

我不知道您是否有權訪問正在加載的頁面,但是可以在母版頁面上創建一個公共屬性,例如currentPage ,然后在頁面本身的Page_Load上將其設置為所需的值。 這樣,您可以在Site.Master代碼上進行檢查:

// on the Site.Master.cs
public string CurrentPage;


// on the page, inside the Page_Load event
((Site)this.Master).CurrentPage = 'My page';


// on the Site.Master
<%
if (CurrentPage == "My page")
{
    %>My page is loaded.<%
}
else
{
    %>Another page is loaded.<%
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM