Page title in ASP.NET pages

Discussion in 'ASP .Net' started by =?Utf-8?B?TWF4aW0gSXpicm9kaW4=?=, Feb 2, 2004.

  1. Hello
    For displaying page titles for my ASP.NET applications I use the following technique
    <title><%=BannerModule.PageTitle%></title
    where BannerModule.PageTitle is a public field of my user control. This field is initialized in Page_Load of every my page. This technique works with the exception that the title disappears every time a page is posted back. Could somebody explain me the reason, or suggest a better technique for setting page title at run-time?
    Thanks a lot
    Maxim
     
    =?Utf-8?B?TWF4aW0gSXpicm9kaW4=?=, Feb 2, 2004
    #1
    1. Advertising

  2. Maxim
    The page title disappears after a post back probably because you did not assign the BannerModule.PageTitle on the page post back. You have to assign that value to the control again for it to display properly. Alternatively, you can place placeholder control between the title tags and in your code you populate the title. That way, you don't have to worry about postback problem if you enable viewstate

    Tu-Thac

    ----- Maxim Izbrodin wrote: ----

    Hello
    For displaying page titles for my ASP.NET applications I use the following technique
    <title><%=BannerModule.PageTitle%></title
    where BannerModule.PageTitle is a public field of my user control. This field is initialized in Page_Load of every my page. This technique works with the exception that the title disappears every time a page is posted back. Could somebody explain me the reason, or suggest a better technique for setting page title at run-time?
    Thanks a lot
    Maxim
     
    =?Utf-8?B?VHUtVGhhY2g=?=, Feb 2, 2004
    #2
    1. Advertising

  3. =?Utf-8?B?TWF4aW0gSXpicm9kaW4=?=

    Coder Coder Guest

    Are you checking if there is a postback in the Page_Load method.

    if (!IsPostBack) {
    }
    else {
    }

    You want to make sure that you set this every time.

    Also you should create a user control to render out the title of the page.
    This will all the pages will have the same title:

    <title>
    <uc1:Banner id="Banner1" runat="server"></uc1:Banner>
    </title>


    public class Banner : System.Web.UI.UserControl
    {
    protected Label Title;

    private void Page_Load(object sender, System.EventArgs e)
    {
    Title.Text = "Hello World";
    }

    #region Web Form Designer generated code
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    this.Load += new System.EventHandler(this.Page_Load);
    }
    #endregion
    }


    http://www.xpcoder.net



    "Maxim Izbrodin" <> wrote in message news:<>...
    > Hello,
    > For displaying page titles for my ASP.NET applications I use the following technique:
    > <title><%=BannerModule.PageTitle%></title>
    > where BannerModule.PageTitle is a public field of my user control. This field is initialized in Page_Load of every my page. This technique works with the exception that the title disappears every time a page is posted back. Could somebody explain me the reason, or suggest a better technique for setting page title at run-time?
    > Thanks a lot,
    > Maxim
     
    Coder Coder, Feb 2, 2004
    #3
  4. Hi Maxim,


    Thanks for posting in the community!
    From your description, you embed an UserControl into a webpage, the
    UserControl has a Public string property so that you can use the value of
    this property to set the UserControl's container page's title via such code:
    <title>
    <%=TitleUC1.PageTitle %>
    </title>
    However, you found that the page's title value will disappear after the
    page is posted back.
    Also, you're looking for anyother approachs to set the page's title, yes?
    If there is anything I misunderstood, please feel free to let me know.

    Based on my research, as for your first problem, I agree to Tu-Thach and
    coder5811's suggestion that you need to set the UserControl's PageTitle
    Property not only when the page is first time loaded but also when posted
    back. For example, use the below code in UserControl:
    if(!IsPostBack)
    {
    PageTitle = "Initial Title";
    }
    else
    {
    //the case when page is posted back
    PageTitle = txtPageTitle.Text;
    }
    Thus, the title won't disappear when the page is posted back.

    As for any other approachs to set the page's title, I found there is
    another means to set the page's title dynamically------------set the
    <titile>element as runat=server and provide an unique id, For example,
    specify the titile in page as below:
    .........
    <HEAD> <TITLE ID="MyTitle" RUNAT="server"> </TITLE> </HEAD>
    ...........

    and also need to declare a HtmlGeneralControl member in the codebehind
    page class like:

    public class TestTitle : System.Web.UI.Page
    {
    .......
    protected System.Web.UI.HtmlControls.HtmlGenericControl MyTitle;
    .............

    Then, we can specify the page's title dynamically by change the MyTitle
    member's InnerText property, just as:
    private void Page_Load(object sender, System.EventArgs e)
    {
    MyTitle.InnerText = "My Custom Title!";
    }

    In addtion, here is weblinks to some tech artiles which have dicussed on
    this topic, you may have a view if you feel any thing unclear:

    #How to Change the Page Title in ASP.NET dynamically
    http://www.dotnetspider.com/Technology/Samples/ShowSample.aspx?SampleId=12

    #Setting the ASP.NET Page Title and Meta Tags
    http://authors.aspalliance.com/chrisg/default.asp?article=141

    Please try out the preceding suggestions. If you have any questions, please
    feel free to let me know.


    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Feb 3, 2004
    #4
  5. Thank you all
    I did assigned the text value to the user control member not only when the page was initially displayed, but also when it was posted back
    private void Page_Load(object sender, System.EventArgs e

    BannerModule.PageName = ResString.IDS_BANNER_TITLE_LOGIN()
    FormsAuthentication.SignOut()

    But it works as I described (the title is lost after when the page is posted back). I've tried the method suggested by Steven Cheng (<TITLE ID="MyTitle" RUNAT="server"></TITLE>) and it works. Thank you, Steven!
     
    =?Utf-8?B?TWF4aW0gSXpicm9kaW4=?=, Feb 3, 2004
    #5
  6. Hi Maxim,


    Thanks for your prompt response. I'm very glad that my suggestion has
    helped you. In addtion, if you meet any other problems or need assitance in
    the furture, please always feel free to post here. I'll be willing to assit
    you. Have a good day!



    Regards,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    Steven Cheng[MSFT], Feb 3, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Klemt
    Replies:
    1
    Views:
    473
    Steve C. Orr, MCSD
    Aug 10, 2003
  2. Replies:
    0
    Views:
    550
  3. Replies:
    1
    Views:
    569
    David
    Apr 24, 2007
  4. soonic
    Replies:
    11
    Views:
    1,800
    soonic
    Jul 27, 2011
  5. Russell
    Replies:
    2
    Views:
    302
    Russell
    Sep 13, 2004
Loading...

Share This Page