[英]How to determine which Child Page is being displayed from Master Page?
我正在母版頁上編寫代碼,我需要知道正在顯示哪個子(內容)頁面。 我該如何以編程方式執行此操作?
我用這個:
string pageName = this.ContentPlaceHolder1.Page.GetType().FullName;
它以這種格式“ASP.default_aspx”返回類名,但我發現很容易為大多數目的解析。
希望有所幫助!
最好讓ContentPage
通知MasterPage
。 這就是為什么ContentPage
有一個Master
屬性和MasterPage
沒有Child
屬性。 最好的方法是在MasterPage
上定義屬性或方法,並通過ContentPage
的Master
屬性使用它。
如果使用此技術,最好明確指定MasterPage的類名。 這使得在ContentPage中使用MasterPage。
例:
//Page_Load
MyMaster m = (MyMaster)this.Master;
m.TellMasterWhoIAm(this);
希望這可以幫助。
這聽起來像個壞主意。 主人的想法是它不應該關心哪個頁面,因為這是每個頁面的所有常用代碼。
我有理由檢查母版頁中的子頁面。
我的母版頁上有所有菜單選項,如果未設置某些系統設置,則需要禁用它們。
如果不是,則顯示消息並禁用按鈕。 由於設置頁面是來自此母版頁的內容頁面,因此我不希望該消息繼續顯示在所有設置頁面上。
這段代碼對我有用:
//Only show the message if on the dashboard (first page after login)
if (this.ContentPlaceHolder1.Page is Dashboard)
{
//Show modal message box
mmb.Show("Warning Message");
}
使用下面的代碼。
Page.ToString().Replace("ASP.","").Replace("_",".")
這是我對問題的解決方案(此代碼進入母版頁后面的代碼):
if (Page.TemplateControl.AppRelativeVirtualPath == "~/YourPageName.aspx")
{
// your code here
}
或者更復雜,但不太可讀:
if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase))
{
// your code here
}
Request.CurrentExecutionFilePath;
要么
Request.AppRelativeCurrentExecutionFilePath;
您可以通過獲取最后一個segmant或請求來完成此操作,我將成為表單名稱
string pageName = this.Request.Url.Segments.Last();
if (pageName.Contains("EmployeeTermination.aspx"))
{
}
你可以嘗試這個:
<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>
將該代碼放在Site.Master
的title
標記內
我在我的一個項目中做了類似的事情,根據正在加載的頁面動態附加css文件。 我只是從請求中獲取文件的名稱:
this.Request.Url.AbsolutePath
然后從那里提取文件名。 如果您正在進行URL重寫,我不確定這是否有效。
string s = Page.ToString().Replace("ASP.directory_name_","").Replace("_aspx",".aspx").Replace("_","-");
if (s == "default.aspx")
{ /* do something */ }
我正在使用這么多答案
<%if(this.MainContent.Page.Title != "mypagetitle") { %>
<%}%>
這樣可以輕松排除任何單個頁面,因為您比較字符串甚至可以為exclude_pagetitle等頁面添加前綴,並比較標題的子字符串。 我通常使用它來從我不想加載的某些功能中排除登錄頁面,如會話超時和實時聊天。
下面的代碼工作就像一個迷人的..它
string PName = Request.UrlReferrer.Segments[Request.UrlReferrer.Segments.Length - 1];
您應該可以從母版頁代碼中使用Page.Request.Url.PathAndQuery或Url Uri對象的其他屬性之一。
您可以在代碼隱藏中檢查頁面類型:
// Assuming MyPage1, MyPage2, and MyPage3 are the class names in your aspx.cs files:
if (this.Page is MyPage1)
{
// do MyPage1 specific stuff
}
else if (this.Page is MyPage2)
{
// do MyPage2 specific stuff
}
else if (this.Page is MyPage3)
{
// do MyPage3 specific stuff
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.