ASP.NET主页面没有执行

时间:2010-08-10 13:40:46

标签: c# asp.net master-pages webforms

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

然而,母版似乎没有执行我定义的任何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>

内容按预期显示,但当代码全部位于单独的页面上时,没有显示任何菜单链接。

有什么想法吗?

3 个答案:

答案 0 :(得分:1)

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

应该更像:

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

因为页面与代码隐藏之间没有链接,这就是为什么没有执行。

答案 1 :(得分:1)

您还需要AutoEventWireup =“true”和CodeFile =“file.master.cs”属性才能显示在母版页的定义中。

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

答案 2 :(得分:1)

Codefile="Admin.master.cs"

检查出来!!