ASP.NET 母版页未执行
我有一个母版页和一个内容页。
然而,母版页似乎没有执行我定义的任何 PageLoad 代码。
此外,当我构建应用程序时,我没有收到任何错误或警告。
这是我的主管理页面:
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="mainHead" runat="server" >
<title>Administration</title>
<link rel="Stylesheet" href="../style/admin.css" />
</head>
<body>
<form id="mainForm" runat="server">
<div class="topMenu">
<asp:Panel id="mnu0" runat="server" CssClass="navButton">
<a href="admin.aspx?mid=0" class="navLink">Admin Home</a>
</asp:Panel>
<asp:Panel id="mnu1" runat="server" CssClass="navButton">
<a href="admin.aspx?mid=1" class="navLink">User Manager</a>
</asp:Panel>
<asp:Panel id="mnu2" runat="server" CssClass="navButton">
<a href="admin.aspx?mid=2" class="navLink">Products</a>
</asp:Panel>
<asp:Panel id="mnu3" runat="server" CssClass="navButtonR">
<a href="../default.aspx" class="navLink">Back to Site</a>
</asp:Panel>
</div>
<br /><br />
<asp:Panel id="subLinks" runat="server" CssClass="subMenu"></asp:Panel>
<div class="mainContent">
<asp:contentplaceholder id="mainContent" runat="server" />
</div>
</form>
</body>
</html>
它的代码隐藏:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace AlphaPackSite.MasterPages
{
public partial class Admin : System.Web.UI.MasterPage
{
protected int menuID;
protected void Page_Load(object sender, EventArgs e)
{
string menuIDdata = Page.Request.QueryString["mid"];
menuID = 0;
// Check the user is allowed here
if (!Roles.IsUserInRole("Admin"))
{
Response.Redirect("../default.aspx");
}
// Get the menu ID
if (!int.TryParse(menuIDdata, out menuID))
{
menuID = 0;
}
// Select the correct menu
var selectedMenu = this.Page.FindControl("mnu" + menuID) as Panel;
selectedMenu.CssClass = "navButtonO";
// Admin menu
if (menuID == 0)
{
subLinks.Controls.Add(new HyperLink { Text = "Admin Home", NavigateUrl = "admin.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Site Settings", NavigateUrl = "siteSettings.aspx", CssClass = "subLink" });
}
// User manager
else if (menuID == 1)
{
}
// Products
else if (menuID == 2)
{
subLinks.Controls.Add(new HyperLink { Text = "Product Categories", NavigateUrl = "productCats.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Organise Products", NavigateUrl = "productOrg.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Add Product", NavigateUrl = "productAdd.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Modify Products", NavigateUrl = "productChange.aspx", CssClass = "subLink" });
}
}
}
}
内容页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="admin.aspx.cs" Inherits="AlphaPackSite.admin"
title="Hi there!"
MasterPageFile="../MasterPages/Admin.master"
%>
<asp:content id="Content1" contentplaceholderid="mainContent" runat="server">
lol this is the admin page
</asp:content>
内容按预期显示,但没有菜单链接显示。当代码全部位于单独的页面上时执行此操作。
有什么想法吗?
I have a master page, and a content page.
The master page however doesn't appear to be executing any of the PageLoad code that I have defined.
In addition, when I built my application I did not get any errors or warnings.
Here is my master admin page:
<%@ Master Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="mainHead" runat="server" >
<title>Administration</title>
<link rel="Stylesheet" href="../style/admin.css" />
</head>
<body>
<form id="mainForm" runat="server">
<div class="topMenu">
<asp:Panel id="mnu0" runat="server" CssClass="navButton">
<a href="admin.aspx?mid=0" class="navLink">Admin Home</a>
</asp:Panel>
<asp:Panel id="mnu1" runat="server" CssClass="navButton">
<a href="admin.aspx?mid=1" class="navLink">User Manager</a>
</asp:Panel>
<asp:Panel id="mnu2" runat="server" CssClass="navButton">
<a href="admin.aspx?mid=2" class="navLink">Products</a>
</asp:Panel>
<asp:Panel id="mnu3" runat="server" CssClass="navButtonR">
<a href="../default.aspx" class="navLink">Back to Site</a>
</asp:Panel>
</div>
<br /><br />
<asp:Panel id="subLinks" runat="server" CssClass="subMenu"></asp:Panel>
<div class="mainContent">
<asp:contentplaceholder id="mainContent" runat="server" />
</div>
</form>
</body>
</html>
It's code behind:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
namespace AlphaPackSite.MasterPages
{
public partial class Admin : System.Web.UI.MasterPage
{
protected int menuID;
protected void Page_Load(object sender, EventArgs e)
{
string menuIDdata = Page.Request.QueryString["mid"];
menuID = 0;
// Check the user is allowed here
if (!Roles.IsUserInRole("Admin"))
{
Response.Redirect("../default.aspx");
}
// Get the menu ID
if (!int.TryParse(menuIDdata, out menuID))
{
menuID = 0;
}
// Select the correct menu
var selectedMenu = this.Page.FindControl("mnu" + menuID) as Panel;
selectedMenu.CssClass = "navButtonO";
// Admin menu
if (menuID == 0)
{
subLinks.Controls.Add(new HyperLink { Text = "Admin Home", NavigateUrl = "admin.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Site Settings", NavigateUrl = "siteSettings.aspx", CssClass = "subLink" });
}
// User manager
else if (menuID == 1)
{
}
// Products
else if (menuID == 2)
{
subLinks.Controls.Add(new HyperLink { Text = "Product Categories", NavigateUrl = "productCats.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Organise Products", NavigateUrl = "productOrg.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Add Product", NavigateUrl = "productAdd.aspx", CssClass = "subLink" });
subLinks.Controls.Add(new HyperLink { Text = "Modify Products", NavigateUrl = "productChange.aspx", CssClass = "subLink" });
}
}
}
}
The content page:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="admin.aspx.cs" Inherits="AlphaPackSite.admin"
title="Hi there!"
MasterPageFile="../MasterPages/Admin.master"
%>
<asp:content id="Content1" contentplaceholderid="mainContent" runat="server">
lol this is the admin page
</asp:content>
Content displays as expected, but none of the menu links display when they did when the code was all on a seperate page.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看看这个!
Check this out!!
应该更像 :
因为页面和代码隐藏之间没有链接,这就是为什么没有执行任何内容。
should be more like :
since there is no link between the page and the code-behind that's why nothing executes.
您还需要 AutoEventWireup="true" 和 CodeFile="file.master.cs" 属性出现在母版页的定义中。
You need the AutoEventWireup="true" and CodeFile="file.master.cs" attributes to appear in the definition of your master page as well.