Categories
Asp, Asp.net

How To Generate Dynamic Page Title In ASP.NET

How To Generate Dynamic Page Title In ASP.NET

In this tutorial we will learn How To Generate Dynamic Page Title In ASP.NET.Here we have already defined page title of the page, we are overloading that through our code.Page control provides page parameters, such as title, metaDescription etc. You can access all theparameters using Page Control in code file. First Check page URL on Page_Load method using Request object. Lets take an simple example.

Design:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>How To Generate Dynamic Page Title In ASP.NET</title>
<style type="text/css">
body
 {
 width: 980px;
 margin: 0px auto;
 text-align: center;
 padding-top: 50px;
 font-size: 20px;
 }
</style>
</head>
<body>
<form id="form1" runat="server">
<h2>How To Generate Dynamic Page Title In ASP.NET</h2>
<div>
    
</div>
</form>
</body>
</html>

Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.Request.Url.ToString() == "http://grootstech.com/demo/How-To-Generate-Dynamic-Page-Title-In-ASP.NET/Default.aspx")
        {
            Page.Title = "How To Generate Dynamic Page Title In ASP.NET C#";
            Page.MetaDescription = "How To Generate Dynamic Page Title In ASP.NET C#";
            Response.Write("<h2>This is a dynamic heading </h2>");
        }

    }
}

demo