views:

79

answers:

3

Hi all,

I have a master page, and a content page.

The master page however doesn't appear to be executing any of the page load code I have defined, when I build it I get no 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"&gt;

<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?

+1  A: 
<%@ 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.

Johan Buret
+1  A: 

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" %>
Joel Etherton
Thanks for your help but I get Compiler Error Message: ASPNET: Make sure that the class defined in this code file matches the 'inherits' attribute, and that it extends the correct base class (e.g. Page or UserControl).
Tom Gullen
@Tom Gullen - the Inherits needs to be the class in your master page "Admin". I'll edit my response to reflect your file names.
Joel Etherton
+1  A: 

Codefile="Admin.master.cs"

Check out dis!!

Vikash
This helped as it showed a better error message, and "var selectedMenu = FindControl("mnu" + menuID.ToString()) as Panel;" worked instead of this.master.Find... Thank you!
Tom Gullen