Vb.net – Setting page title from @ Page directive in ASP.NET using master pages

asp.netmaster-pagesvb.net

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">
<html xmlns="http://www.w3.org/1999/xhtml">

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

Best Answer

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 :)

Related Topic