Muhammad Mamoor Khan
Muhammad Mamoor Khan

Reputation: 502

ASP.NET / C# Page Title

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

Upvotes: 1

Views: 8258

Answers (5)

Muhammad Mamoor Khan
Muhammad Mamoor Khan

Reputation: 502

here is my answer so that anyone having this kind of stupid situation can gain some wisdom from my experience, after banging my head to the wall for a week I think this cannot be accomplished.

Upvotes: 0

Ryan Cook
Ryan Cook

Reputation: 41

Why not just use a literal control?

    Literal ltPageTitle = (Literal)Master.FindControl("ltPageTitle"); // If the literal is on a master page 
    // Literal ltPageTitle = (Literal)Page.FindControl("ltPageTitle"); 
    ltPageTitle.Text = "Title"; // Where Title is the title you want for the page

This even gives you extra functionality such as using a database record to name your page or whatever else you can think of.

Upvotes: 0

user3457341
user3457341

Reputation: 1

If you are using master page, try this:

In the master page, go to the head and put:

<head runat="server" id="yourHead">
       <asp:ContentPlaceHolder runat="server" id="holderHead"></asp:ContentPlaceHolder>
       <title>Your Title</title>
</head>

Now, in the other pages html code put this:

<asp:Content ID="content" ContentPlaceHolderID="holderHead" runat="Server">
   <title>Your Title - Contact</title>
</asp:Content>

It worked for me.

Ps:. Sorry for my bad english!

Upvotes: 0

LNRao
LNRao

Reputation: 169

You can write in page_load like

this.Page.Title = YourTitle;

Upvotes: 3

Jonathan Henson
Jonathan Henson

Reputation: 8206

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.

Upvotes: 4

Related Questions