Using Title Attribute in ASP.NET

Introduction

Page Title is fully depend on the separate page itself. If we have a content page of any master page then we can write title in separate page of separate title. If we not write title in content page then Master Page title will automatically displayed. For example, the code given below is displaying the title separately for content page.

<%@ Page Title="MINDCRACKER" Language="VB"
MasterPageFile="~/MasterPage/MasterPage.master"
AutoEventWireup="false" CodeFile="Default.aspx.vb"
Inherits="MasterPage_Default" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<
asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
</asp:Content>

Now if we delete Title from above code and run the project then title will displayed by default from Master Page. Here is the master page with title.

<%@ Master Language="VB" CodeFile="MasterPage.master.vb" Inherits="MasterPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>MINDCRACKER _MASTER_PAGE_TITLE</title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
    </div>
    </form>
</body>
</
html>

Remember, if we write Title in both Master Page and Content Page simultaneously then Content Page Title will be displayed.

We have also option to display title of any page from code-behind. We can use Page_Load() event for that. For example, given below is code-behind for Default.aspx file that is Default.aspx.vb.

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.Title = "MINDCRACKER _PAGE_LOAD_TITLE"
    End Sub
End Class

Here is the code of Default.aspx page.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Temprory Title</title>
</head>
<
body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>
</
html>

Remember, if we write Title in Page and Code-behind both simultaneously then Page_Load() title will be displayed.

HAVE A GREAT CODING!

Up Next
    Ebook Download
    View all
    Learn
    View all