views:

2504

answers:

4

I am using master pages and am having trouble setting page titles from the @ Page directive. All of my classes inherit from a class myPage which inherits from the ASP.NET System.Web.UI.Page class. Please Note: I have runat="server" set in my master page's head tag.

Here's what my @ Page directives look like for the file test.aspx.vb:

<%@ Page language="VB" MasterPageFile="~/MainMaster.master" 
autoeventwireup="false" CodeFile="test.aspx.vb" 
Inherits="test" Title="test" %>

Here's what test.aspx.vb looks like:

Partial Class test
    Inherits myPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

This is what my master file, MainMaster.master, looks like:

<%@ Master Language="VB" CodeFile="MainMaster.master.vb" Inherits="MainMaster" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"&gt;

<head runat="server">
    <title>untitled</title>
</head>
...

Now, when you go to view test.aspx in a browser you'd expect to see the title 'test'. but instead you will see 'untitled' as per the master page. Through trial and error, I modified the test class to inherit from System.Web.UI.Page directly, instead of myPage like this:

Partial Class test
    Inherits Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

End Class

and everything worked just fine. Why would my pages being children of myPage instead of System.Web.UI.Page prevent the title from being set correctly in the @ Page directive?

I realize that I could just set the page titles programmatically through the *Page_Load* methods in every page, but I'd rather do it in the @ Page directives in the .aspx files.

This is a very weird and frustrating problem, and I'm at a loss!

Thanks!!!

A: 

I have a very similar set up to you. I have content pages inheriting from a custom base page which is itself inheriting from page. I am not having any issues with the title being set on the aspx and being shown in the browser. The only difference I see between my code and yours is my master page has the autoeventwireup property where your master page does not, and also your master page has a property called codefile and mine has codebehind.

Content Page:

<%@ Page Title="Login to Application X" Language="vb" AutoEventWireup="false" MasterPageFile="~/masterpages/mymasterpage.Master"
    CodeBehind="login.aspx.vb" Inherits=".login" %>

Master Page:

<%@ Master Language="VB" AutoEventWireup="false" CodeBehind="mymasterpage.master.vb"
    Inherits=".mymasterpage" %>
Dan Appleyard
Are you using VB or C#?
John Sheehan
Thanks for your reply, Dan. I tried adding AutoEventWireup="false" to my master page with no results. Regarding codebehind vs codefile, apparently codefile has to do with as change made in .NET 2.0: http://www.ddj.com/windows/187202007
wilsoniya
I am using VB. Specifically i am using .net 2.0 framework within vs 2008 ide.
Dan Appleyard
+1  A: 

What methods do you have in your base page (myPage.vb).

If you are overriding any of the default methods, are you calling the base versions of those pages?

in C# I'd have something like this:

protected override void OnInit(EventArgs e)
{
    // Do my custom processing.

    // Don't forget to call base OnInit here:
    base.OnInit(e);
}

If you don't call these methods, then the events that happen in them for you (like wiring up the masterpage's title) won't fire.

Zhaph - Ben Duguid
Good suggestion. I actually checked this, and the myPage class does not override any of the Page class's methods--it only adds functionality. Hilariously, I created an empty child of Page and had the test class inherit from that, and everything worked fine. There must be a problem with myPage...
wilsoniya
+2  A: 

I thank everyone for their help; I have found a solution. The problem was that the myPage class had a property for Title, but in the Set part of the property was not passing the changes on to Page.Title as it should have been.

A one line change fixed my problem :)

wilsoniya
A: 

Thanks a lot. It was so simple..