skip to Main Content

I am assigning the page title from code behind using C#. All works fine! But, just the thing is the Title Text is displayed with LINE BREAK.

So, how to prevent the Line Break of the Title?

phonesOnly.aspx

<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage_New.master" AutoEventWireup="true" CodeFile="phonesOnly.aspx.cs" Inherits="phonesOnly" %>

phonesOnly.aspx.cs

protected void SetTitle()
{
 this.Page.Title = "Sell Your Cell Phone Online or at our Los Angeles locations";
}

View Source of Page:

<title>
    Sell Your Cell Phone Online or at our Los Angeles locations 
</title>

Expected Rendering without Line Break:

<title>Sell Your Cell Phone Online or at our Los Angeles locations</title>

NOTE:
I know I can be done with <title id="mytitle" runat='server'></title> but I am Restricted to use runat=server. May be due to SEO perspective.

Please suggest any other solution?

2

Answers


  1. Unfortunately, as soon as you add a runat=”server” to the <head> tag, this is what happens. The HtmlHead class parses your content inside the head tag, then rewrites it, and it unfortunately messes it up when it does so. The only way to get properly formatted output is to avoid using a server side head tag, which means you cannot use the Page.Title property, so you have to use your own title variable, but you can remove the id by assigning null to the id in the code behind.

    The following works as expected:

    <head> <!-- note no runas="server" -->
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title runat="server" id="myTitle"></title>
    ...
    </head>
    

    Then in code behind:

    myTitle.InnerText = "My Page Title";
    myTitle.ID = null;
    

    This generates the following HTML.

    <head>
      <meta charset="utf-8" />
      <meta name="viewport" content="width=device-width, initial-scale=1.0" />
      <title>My Page Title</title>
      ...
    </head>
    

    This has the advantage that it doesn’t bunch up all your meta tags on one line either.

    If you’re using a Master page, then you’ll have to do a little hoop jumping to get the control from the master page instead. Either create a property on the Master page to return the control, or use FindControl like this:

    var ctl = Master.FindControl("myTitle") as HtmlGenericControl;
    ctl.InnerText = "My Page Title";
    ctl.ID = null;
    

    Make sure you have not set the ID to null in the master page, or the FindControl won’t work in the content page.

    Login or Signup to reply.
  2. My team ran into the same issue. We fixed it by wrapping the original HtmlTitle object and stripping the line breaks.

    Wrapper class:

    /// <summary>
    /// Exists to remove the newlines created by asp.net
    /// </summary>
    public class TrimmedHtmlTitle : HtmlTitle
    {
        private readonly HtmlTitle _title;
        public TrimmedHtmlTitle(HtmlTitle title)
        {
            _title = title;
        }
    
        protected override void Render(HtmlTextWriter output)
        {
            if (_title == null)
            {
                return;
            }
    
            try
            {
                using (var stringWriter = new System.IO.StringWriter())
                {
                    using (var htmlTextWriter = new HtmlTextWriter(stringWriter))
                    {
                        _title.Text = Text;
                        _title.RenderControl(htmlTextWriter);
                        var content = stringWriter.ToString().Replace("r", "").Replace("n", "").Replace("t", "").Trim();
                        output.Write(content);
                    }
                }
            }
            catch { }
        }
    }
    

    Setup and use case:

            var head = page.Controls.OfType<HtmlHead>;
            if (head != null)
            {
                var htmlTitle = head.Controls.OfType<HtmlTitle>().FirstOrDefault();
                if (htmlTitle != null)
                {
                    var index = head.Controls.IndexOf(htmlTitle);
                    head.Controls.Remove(htmlTitle);
                    head.Controls.AddAt(index, new TrimmedHtmlTitle(htmlTitle));
                    head.Title = "MyTitle";
                }
            }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search