ASP.NET 母版页未执行

发布于 2024-09-13 11:55:14 字数 4241 浏览 3 评论 0原文

我有一个母版页和一个内容页。

然而,母版页似乎没有执行我定义的任何 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

意中人 2024-09-20 11:55:14
Codefile="Admin.master.cs"

看看这个!

Codefile="Admin.master.cs"

Check this out!!

天赋异禀 2024-09-20 11:55:14
<%@ Master Language="C#" %>

应该更像 :

<%@ Master Language="C#" CodeFile="Admin.master.cs" Inherits="Admin" %>

因为页面和代码隐藏之间没有链接,这就是为什么没有执行任何内容。

<%@ Master Language="C#" %>

should be more like :

<%@ Master Language="C#" CodeFile="Admin.master.cs" Inherits="Admin" %>

since there is no link between the page and the code-behind that's why nothing executes.

浅语花开 2024-09-20 11:55:14

您还需要 AutoEventWireup="true" 和 CodeFile="file.master.cs" 属性出现在母版页的定义中。

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

You need the AutoEventWireup="true" and CodeFile="file.master.cs" attributes to appear in the definition of your master page as well.

<%@ Master Language="C#" AutoEventWireup="true" CodeFile="Admin.master.cs" Inherits="Admin" %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文