how to change title of aspx page dynamically on page load

和自甴很熟 提交于 2019-12-05 19:25:41

问题


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.


回答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
      }
}



回答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




回答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.




回答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




回答5:


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



来源:https://stackoverflow.com/questions/16982811/how-to-change-title-of-aspx-page-dynamically-on-page-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!