.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

How to set page title of child page from the master page's code behind, in ASP.Net

Posted By:ASPEvil       Posted Date: July 31, 2014    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

How we can change the tile of a child page of ASP.Net from the ASP.Net Master page.
 

In some cases, we need to change the page title of the child page from the master pages code behind of ASP.Net. This article shows you how to do this.

With the Page_Load event of the master page, write down the following code:-
-------------------------------
[VB.Net code]

If Not IsPostBack Then
            Dim chldPage As String = Page.AppRelativeVirtualPath

            If chldPage = "~/Default.aspx" Then
                Page.Title = "Directory of Victoria - Home"
            End If

            If chldPage = "~/sample-pages.aspx" Then
                Page.Title = "Directory of Victoria - Sample Pages"
            End If

            If chldPage = "~/login.aspx" Then
                Page.Title = "Directory of Victoria - Log In"
            End If

            If chldPage = "~/logout.aspx" Then
                Page.Title = "Directory of Victoria - Log Out"
            End If

            If chldPage = "~/directoryindex.aspx" Then
                Page.Title = "Directory - Directory Index"
            End If

        End If

-------------------------------

[C# code]

------------------
if (!IsPostBack) {
    string chldPage = Page.AppRelativeVirtualPath;
    if ((chldPage == "~/Default.aspx")) {
        Page.Title = "Directory of Victoria - Home";
    }
    if ((chldPage == "~/sample-pages.aspx")) {
        Page.Title = "Directory of Victoria - Sample Pages";
    }
    if ((chldPage == "~/login.aspx")) {
        Page.Title = "Directory of Victoria - Log In";
    }
    if ((chldPage == "~/logout.aspx")) {
        Page.Title = "Directory of Victoria - Log Out";
    }
    if ((chldPage == "~/directoryindex.aspx")) {
        Page.Title = "Directory - Directory Index";
    }
} 

------------------


With the above code, when the master page loads, its Page_Load event checks from which child page( I am having the child pages as: Default.aspx, sample-pages.aspx, login.aspx, logout.aspx and directoryindex.aspx. All these child pages are at the root directory/folder of the ASP.Net website), this master page is being called, then as per the called child page, the above mentioned code changes the title of the master page, as the title predefined with the same code. Then with the other visual elements of the master page, this title of the master page is also shown on the child page.

Important note: Do not declare any title for the child pages which you are using with the above mentioned code of the master page, else the final title will be shown with the child page, will be the title declared with the child page, not the tile declared with the above mentioned code of the master page.



Thanks.
 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend