1

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

  • 2
    what's there ? it's same only. – Ravi Gadag Mar 04 '13 at 07:18
  • Can you ask your manager why it should render differently? – Ravi Y Mar 04 '13 at 07:19
  • Related: http://stackoverflow.com/a/255018/55209 – Artem Koshelev Mar 04 '13 at 07:21
  • If you use ASP.Net webform, it's to hard to reformat what has been rendered. – Habibillah Mar 04 '13 at 07:22
  • @Ravi: I know its the same, but Boss is always right – Muhammad Mamoor Khan Mar 04 '13 at 07:47
  • Sorry for the incomplete comment, enter got pressed accidentally. Ravi: I know its the same, but Boss is always right ryadavilli: same as above (I guess its due to some SEO technique being affected) ArtemKoshelev: well that didn't get caught in my search results, guess I am newbie here, will try broader search terms next time before asking questions Habibillah: I don't think its that hard to format ASP.NET controls rendering in the first place, but sometimes we are stuck at negligible problems that we don't want neglect :) – Muhammad Mamoor Khan Mar 04 '13 at 07:57
  • I'm often doing the same, i usually have a sqldatasource on my page, – Michiel Mar 04 '13 at 07:20

5 Answers5

4

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.

Jonathan Henson
  • 8,076
  • 3
  • 28
  • 52
  • oh it is causing the tag to be rendered twice as one tag is coming from the Master Page :( – Muhammad Mamoor Khan Mar 04 '13 at 08:52
  • Wish I could upvote your answer for the humor but I ain't got that privileges yet :| – Muhammad Mamoor Khan Mar 04 '13 at 10:09
  • You could just move the code-block to the master page, then move this code to the master page as well. You could also just put the property in the master page. You can access the masterpage class from your page class. – Jonathan Henson Mar 04 '13 at 13:56
  • that's what I couldn't figure out that how to access the public properties of master class in a child page – Muhammad Mamoor Khan Mar 04 '13 at 18:13
  • @MuhammadMamoorKhan this should help. http://dotnetbyexample.blogspot.com/2007/10/right-way-of-accessing-master-page.html – Jonathan Henson Mar 04 '13 at 21:26
  • let me try it out but the manager was OK with the effort I put into this small thing and handled it on his own, however he won't tell me whether he successfully accomplished the task or not :) PS: should I accept your answer or not? (btw its not working either) – Muhammad Mamoor Khan Mar 11 '13 at 17:46
  • @MuhammadMamoorKhan just a shot in the dark here. Have you tried trimming the Page Title to remove and preceding and trailing '\n' characters? – Jonathan Henson Mar 12 '13 at 16:31
  • Nope I haven't and to be sure can I get the results using .NET Frameworks builtin string function .Trim() ??? – Muhammad Mamoor Khan Mar 13 '13 at 17:52
3

You can write in page_load like

this.Page.Title = YourTitle;
LNRao
  • 169
  • 4
0

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.

miken32
  • 42,008
  • 16
  • 111
  • 154
0

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!

0

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.

Ryan Cook
  • 41
  • 1
  • 3