how to change title of aspx page dynamically on page load
Asked Answered
C

6

13

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.

Chisolm answered 7/6, 2013 at 11:16 Comment(0)
T
19

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
      }
}
Tenuous answered 7/6, 2013 at 11:31 Comment(0)
L
5

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

Lineate answered 7/6, 2013 at 11:33 Comment(0)
A
4

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.

Alicaalicante answered 30/3, 2017 at 19:37 Comment(0)
Y
2
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

Yvoneyvonne answered 28/4, 2015 at 16:10 Comment(0)
K
0

In your master page code behind, you could set [this.Title = "Whatever";] or you could also specify the default title in the HTML.

Knockabout answered 7/6, 2013 at 11:22 Comment(0)
E
0

I wanted to change the title of the home page same as you. In the main file in your hosting, which contains all the home page code, the name may differ on different websites. Open it and then look for the title tag inside the header tag. Which is as follows and you can change it to your liking. If you want to write something under the title, like what I did on page Irpolymer, you have to add a

tag and write the text you want, then you can style it or change its size and then save. Consider taking a backup of your service before making changes so that you can restore your site in the event of a problem.

 <head runat="server">
 <title>Untitled Page</title>
 <p>something you want</p>
 <asp:ContentPlaceHolder id="head" runat="server">
 </asp:ContentPlaceHolder>
 </head>
Expunction answered 19/7, 2020 at 5:54 Comment(1)
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)Cargo

© 2022 - 2024 — McMap. All rights reserved.