how to change title of aspx page dynamically on page load

47,971

Solution 1

If this is classic ASP.NET (not MVC) and you are using MasterPage then you can set default title in Page_Load event in MasterPage:

protected void Page_Load(object sender, EventArgs e)
{
      if (string.IsNullOrEmpty(Page.Title))
      {
           Page.Title = ConfigurationManager.AppSettings["DefaultTitle"];  //title saved in web.config
      }
}

Solution 2

You can do this:

Set the aspx header something like this

<HEAD> 
<TITLE ID=CaptionHere RUNAT="server"></TITLE> 
</HEAD> 

And in code behind put this inside the page load event:

if(!IsPostBack)
{
  myCaption.InnerHtml = "Hope this works!"
}

I hope this will help you

Solution 3

I had a similar problem and none of these solutions worked well for me. The problem stems from the order control events fire for a page. In my case, I had some code that needed to be in the Page_load event (this was because that is the first event where we have a Request object to work with). That code also needed to run before the title could be set. Other pages in my site were able to simply set the desired Title in the page Ctor but because this page needed to interrogate the response object for information first, it was a problem. The problem with this is that the master page has already created the page header section by the time we get to the Page_load event and I didn't want junk in my Master page that was only required for a single page on my site. My simple hack to overcome this issue was to insert a bit of javascript inline in the content portion of the page:

<asp:Content ID=BodyContent ContentPlaceHolderID=MainContent RunAt=Server>
    <script type="text/javascript">
        document.title='<%=Title%>';
    </script>

    ... the rest of the content page goes here ...

</asp:Content>

With this in place, you are free to set the Title in the Page_Load event and it'll be set as soon as this line of code has downloaded. Of course, my site already has a JS requirement so if you're trying to avoid that then this is not going to work for you.

Solution 4

protected void Page_Load(object sender, EventArgs e)
{
     Page.Title = title();
}
private string title()
{

    SqlConnection con = new SqlConnection(cs);
    string cmdstr = "select * from title where id = 2";
    SqlCommand cmd = new SqlCommand(cmdstr, con);
    DataTable dt = new DataTable();
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    con.Open();
    da.Fill(dt);
    con.Close();
    if (dt.Rows.Count > 0)
    {
        string title = dt.Rows[0]["title"].ToString();
    }
    return title;
}

This is helpful

Share:
47,971
Randhi Rupesh
Author by

Randhi Rupesh

Interest in learning new technology and having some basic knowledge on dotnet and html

Updated on July 19, 2020

Comments

  • Randhi Rupesh
    Randhi Rupesh almost 4 years

    I had set of ASPX pages in which each page had different titles, but I want to put default title for pages which don't have a title. The default title must be configurable.

  • Mathias
    Mathias almost 4 years
    Welcome to SO. While this answer is true for manually changing the title. This question is rather about how to change the title through code. (e.g. changing the title based on the user that is accessing the page)