Search code examples
asp.nethtmlcode-behind

Additional characters to change the tag "title" in the code behind


I need to change the page title according to its contents, then I perform the following:

HTML (aspx):

<title id="titleOfPage" runat="server"></title>

Code behind

titleOfPage.Text = news.Title;

But when I run the page, I see the following:

<title>
    My custom title completely
</title>

My custom title has the following characters "0D" + "0A"+ "09", or ASCII eguivalente "013"+ "010""009" (carriage return + new line + horizontal tab) at the start, and "0D" + "0A"at the end.

How I can do to display correctly?, I need it to search engines (Google, Bing, ...).

news.Title is correct, no spaces, its value is "My custom title completely".

Thanks

Edit

I tried titleOfPage.Text = "My custom title completely"; and I get the same result

Edit II

I tried creating a property in the codebehind, but the result is the same, however if I put the property on a tag "div" text looks good.

codebehind:

...
public string TitleOfPage { get; private set; }
...

TitleOfPage = news.Title;

HTML:

<head>
    <title><%= TitleOfPage %></title>
    ...
</head>
<body>
    <div><%= TitleOfPage %></div>
    ...
</body>

Result:

<title>
    My custom title completely
</title>

<div>My custom title completely</div>

EDIT III

First sorry for not putting this before. in my example I did not fully included the tag head, the full head tag is as follows <header runat="server">, I do not know why but if I remove runat="server", it works correctly.

Then appeared the property "innerText" to the title:

HTML:

<title id="TitleOfPage" runat="server"></title>

Codebehind:

TitleOfPage.InnerText = news.Title;

Thanks.


Solution

  • Are you saying ASP.NET is adding those characters to your title? That doesn't sound right.

    BTW, I would remove your server-side title tag and setting Page.Title instead.

    Looking at some of my web pages, I see some of them do have newlines places around my title text. If that's all that's happening, then I don't see a problem. White space is essentially removed by the browser and this causes no adverse effects. Is this causing a problem?