如何确定从母版页显示哪个子页面?
我正在母版页上编写代码,我需要知道正在显示哪个子(内容)页面。 我该如何以编程方式执行此操作?
这听起来像个坏主意。 主人的想法是它不应该关心哪个页面,因为这是每个页面的所有常用代码。
我用这个:
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("_",".")
您可以使用:
Request.CurrentExecutionFilePath
这是我对问题的解决方案(此代码进入母版页后面的代码):
if (Page.TemplateControl.AppRelativeVirtualPath == "~/YourPageName.aspx") { // your code here }
或者更复杂,但不太可读:
if (Page.TemplateControl.AppRelativeVirtualPath.Equals("~/YourPageName.aspx", StringComparison.OrdinalIgnoreCase)) { // your code here }
我在我的一个项目中做了类似的事情,根据正在加载的页面动态附加css文件。 我只是从请求中获取文件的名称:
this.Request.Url.AbsolutePath
然后从那里提取文件名。 如果你正在进行URL重写,我不确定这是否会起作用。
您可以通过获取最后一个segmant或请求来完成此操作,我将成为表单名称
string pageName = this.Request.Url.Segments.Last(); if (pageName.Contains("EmployeeTermination.aspx")) { }
你可以尝试这个:
<%: this.ContentPlaceHolder1.Page.GetType().Name.Split('_')[0].ToUpper() %>
将该代码放在Site.Master
的title
标记内
Request.CurrentExecutionFilePath;
要么
Request.AppRelativeCurrentExecutionFilePath;
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等页面添加前缀,并比较标题的子字符串。 我通常使用它来从我不想加载的某些function中排除登录页面,如会话超时和实时聊天。
下面的代码工作就像一个迷人的..它
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 }