ASP.NET / C# Page Title

Muhammad Mamoor Khan picture Muhammad Mamoor Khan · Mar 4, 2013 · Viewed 7.8k times · Source

OK I know this is a stupid question and I really don't know what or how to search for this problem. The problem is that I am storing the page Title values into database and then retrieving the values from database and assigning that title to the page using this piece of code this.Title = pageTitle; and it is rendering as following

    <head><title>
          page title here
    </title>

but my manager wants me to make it render as following

    <head><title>page title here</title>

I don't have any idea what to search or how to do it :( I am using ASP.NET 4 and C# 4 over IIS 6 and Windows Server 2003 (SQL Server 2008 R2 if that helps)

EDIT: I have tried

     <head><title><asp:Literal ID="ltrlMasterTitle" runat="server" Text=""</asp:Literal></title>

And setting it to my desired value using the following code

    Literal lblMasterTitle = (Literal)this.Page.Master.FindControl("lblMasterTitle");
    if (!string.IsNullOrWhiteSpace(pageTitle))
        lblMasterTitle.Text = pageTitle;

but it also renders the same way. PS: I tried to use the solution suggested by Jonathan Hanson but I couldn't figure out the transfer of data between master page and the child page :/

ANOTHER EDIT: I have tried the method suggested in Jonathan Hanson's link but that also render the same o.O

Answer

Jonathan Henson picture Jonathan Henson · Mar 4, 2013

One option would be to use an embeded code-block like this:

<head><title><%=PageTitle%></title>

Then in your code behind:

public String PageTitle
{
  get;
  set;
}

then...

PageTitle = pageTitle;

That should do the trick--albeit kind of ugly. Then again, that is what managers get for micromanaging stupid crap like this.