Wednesday, September 8, 2010

Handling Head Section Dynamically

When it comes to Search Engine Optimization there are few modifications we need to do in Header Section. If a web application has simple structure then handling Head Section is an easy task. But consider an application like E-Commerce store where there is huge collection of different kind of products which belongs to various categories and sub categories. In that case we have to manipulate many things for every request depending on the product, its details, depending on category and subcategory. ( URL Rewriting is one the important thing for Search engine optimization, which I have already discussed in some articles, Article 1, Article 2).
Head section also plays an important role for Search Engine Optimization, in this article we will discuss all elements which matters in HEAD section and can be handled dynamically in asp.net.
  • Handling Page Title: Page title can contain many things explaining details or abstract about the Product or about the current page which is being displayed for some purpose. User might click on some link and page is being redirected to display some product or category. In that case Page Title should also be change accordingly. This can directly be handled as follow


    protected void Page_Load(object sender, EventArgs e)
        {
            this.Header.Title = "This is Demo Title";
            //we can also use following
            //Page.Header.Title or Page.Title or this.Title
            //Can also apply some switch case, depending on Query String.
            //depending on product or category details
        }
  • Adding JavaScript in Head Section: We can either load direct JavaScript in HEAD section or we can add JS file in HEAD Section. check out following codes


    protected void Page_Load(object sender, EventArgs e)
        {
            HtmlGenericControl HTMLControl = new HtmlGenericControl();
            HTMLControl.TagName = "script";
            HTMLControl.Attributes.Add("type", @"text\javascript");
            HTMLControl.InnerText = @"alert('Insert Your JavaScript as String HERE instead of this Alert')";
            this.Page.Header.Controls.Add(HTMLControl);
        }

    Also we can add JavaScript file reference as below.



    protected void Page_Load(object sender, EventArgs e)
        {
            HtmlGenericControl HTMLControl = new HtmlGenericControl();
            HTMLControl.TagName = "script";
            HTMLControl.Attributes.Add("type", @"text\javascript");
            HTMLControl.Attributes.Add("src", "yourJSFile.js");
            this.Page.Header.Controls.Add(HTMLControl);
       }
  • To add content Meta Type in HEAD Section we can use following code.


    protected virtual void AddMetaContentType()
    {
    HtmlMeta meta = new HtmlMeta();
    meta.HttpEquiv = "content-type";
    
    meta.Content = Response.ContentType + "; charset=" + Response.ContentEncoding.HeaderName;
    
    Page.Header.Controls.Add(meta);
    
    }
    
  • To Add Meta Section, e.g. For Keyword and Description we can use following function

    protected void AddMetaTag(string name, string value)
      {
       if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(value))
        return;
    
       HtmlMeta meta = new HtmlMeta();
       meta.Name = name;
       meta.Content = value;
       Page.Header.Controls.Add(meta);
      }
    
Submit this story to DotNetKicks

0 comments: