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.
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
}
}
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
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.
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
In your master page code behind, you could set [this.Title = "Whatever";]
or you could also specify the default title in the HTML.
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>
© 2022 - 2024 — McMap. All rights reserved.