Change page title at runtime?

kcwallace

Centurion
Joined
May 27, 2004
Messages
175
Location
Austin, TX
THis is probably a really dumb question, but is there another way other than the code below to dynamically change a page title at run time?

Visual Basic:
    Protected Overrides Sub Render(ByVal writer As HtmlTextWriter)
        writer.RenderBeginTag(HtmlTextWriterTag.Title)
        writer.Write("ASFD")
        writer.RenderEndTag()
        writer.WriteLine()
    End Sub

When I use the code above it overrides all the labels and text boxes on the form.
 
Page.RenderChildren(writer) I think is what you are looking for where you currently have writer.Write("ASDF")...didn't even know that enumeration exisited...don't do much of that sort of thing....someday I'll know it all :)....like I said earlier, we all need that kick...thanks!
 
I tried your suggestion, and it generates the following error:

... is not accessable in this context because it is 'protected'

I placed it in the "Page_Load" function. Based on the error that is not the correct spot. Where should it be.
 
in Onload do this -

me.RegisterClientSideScript(string.format( "<SCRIPT language=""javascript"">document.title = ""{0}""; </SCRIPT>", sDocTitle)
 
Joe Mamma said:
in Onload do this -

me.RegisterClientSideScript(string.format( "<SCRIPT language=""javascript"">document.title = ""{0}""; </SCRIPT>", sDocTitle)
Only problem with this is if the user has disabled JavaScript... But then again, most websites nowdays use a lot of JavaScript and assume it will be available...
 
Joe Mamma said:
in Onload do this -

me.RegisterClientSideScript(string.format( "<SCRIPT language=""javascript"">document.title = ""{0}""; </SCRIPT>", sDocTitle)

Is there anything I need to import to make that line work. I get the following error:

'RegisterClientSideScript' is not a member of 'myProjectName'
 
I really would suggest that you avoid using JavaScript to accomplish this.

I personally use MasterPage templates for my site which makes it very easy to dynamically change the title. If you dont want to do this, rather have a server side variable called PageTitle or something, and set it in your PageLoad. Then in your .aspx file, you can have <head><title><%=PageTitle%></title></head>

I havent tried this, but it should work...
 
The right way to do this is to make your title tag "visible" to your code using HtmlGenericControl:

html:
<title runat="server" id=pageTitle>My Page</title>

code:
protected System.Web.UI.HtmlControls.HtmlGenericControl pageTitle;

protected override void OnLoad(EventArgs e)
{
base.OnLoad (e);
pageTitle.InnerHtml = "My Title from Code";
}

Greets
Adam
 
Back
Top