Set Text Inside Title Bar in ASP.Net Via Single File

Introduction

Sometimes a client wants that the same text show in the title bar of every page for the entire website, not the variety of text on every page. You can do this in one of 2 ways.

  1. Through Server Side using Web.Config
  2. Through Client Side using JavaScript

Note:

To do so in both cases you need to use a common page, either a Master Page or a Web User Control.

To learn more about this, check the following procedure.

Add a new "Website" named "Website1".



As I have said you earlier, you can do this in one of 2 ways. So here is both ways.

  1. Through the server Side using Web.Config

    This way is server-side because you need to use the server code to access the key from the Web.Config file. You can add a tag with "Key" and "Value" in the <appSettings> section of Web.Config file as in the following code:
  1. <configuration>    
  2.   <appSettings>  
  3.     <add key="Title" value="MY Website Title"/>  
  4.   </appSettings>    
  5.   <system.web>  
  6.     <compilation debug="true" targetFramework="4.0"/>  
  7.   </system.web>  
  8. </configuration> 

Here Key is the "Title" and its value is "MY Website Title".



Now you can access this key in a common file such as a master page and any user control file like header or footer.

Matser Page:
Add a Master page named "Masterpage.master" to the website.



And now add the following code on page_Load event in the .cs file of the Master Page.

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.    Page.Title = System.Configuration.ConfigurationManager.AppSettings["Title "].ToString();  

Here the "Title" name should be the same as what you created in the Web.Config file.

Now add the Web Form named "MyPage.aspx" to the website as in the following:



And select the master page as in the following:



Now add some text onto the page only for identification and run it.



Web User Control:
Add a Web User Control named "MyControl.acsx" to the website.



And now add the following code for the page_Load event in the .cs file of the Web User Control.

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     Page.Title = System.Configuration.ConfigurationManager.AppSettings["Title "].ToString();  

Here the "Title" name should be the same as what you created in the Web.Config file.

Add a Web Form named "Default.aspx" to the website without adding a master page.



Add the "MyControl.acsx" to the "Default.aspx" page as in the following:



Now run the page.


Through Client Side using JavaScripts

This way is client-side because JavaScript is used to set the title of the page. There is no need to change the Web.Config file. It is a very simple way compared to the first way.

Just write the single line of the following code in the <Script> tag:

document.title = " MY Website Title";

Now you can write this code in a common file, it can be a master page and any user control file like header or footer.

Matser Page:
Add a Master page named "Masterpage.master" to the website.



And now add the following code in the <script> tag in the <head> tag of the "MatserPage.master".

  1. <script type="text/javascript">  
  2.     document.title = " MY Website Title";  
  3. </script> 



Now add the web form named "MyPage.aspx" to the website.



And select the master page.



Now add some text on page only for identification and run it.



Web User Control: Add a Web User Control named "MyControl.acsx" to the website.



And now add the following code to the <script> tag in the <body> tag of "MyControl .acsx".

  1. <script type="text/javascript">  
  2.    document.title = " MY Website Title";  
  3. </script> 

Add a Web Form named "Default.aspx" to the website without adding a master page.



Add the "MyControl.acsx" to the "Default.aspx" page.



Watch this script here:



Now run the page.


Similar Articles