Solved

Meta info from codebehind

Posted on 2010-09-12
14
619 Views
Last Modified: 2013-12-17
Hi.

To change my meta info, I do like this:
Page.Title = Name;
HtmlMeta hm1 = new HtmlMeta();
HtmlMeta hm2 = new HtmlMeta();
hm1.Name = "description";
hm1.Content = MetaDescription;
hm2.Name = "keywords";
hm2.Content = MetaKeywords;
HtmlHead head = (HtmlHead)Page.Header;
head.Controls.Add(hm1);
head.Controls.Add(hm2);

Question 1)
How can I isolate it in a class?

Question 2)
In view source, it looks like this:
<meta name="description" content="my content" /><meta name="keywords" content="my content" /><title>
      my title
</title>

Hos does I make it look like this:
<title>mytitle</title>
<meta name="description" content="my content" />
<meta name="keywords" content="my content" />
0
Comment
Question by:bongii
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 33656151
Q1) You can define a method in a class and pass the Page as a parameter to that function.

Q2) What are the differences between these two? Just a line break?
0
 

Author Comment

by:bongii
ID: 33656162
Q1) Do you have an example?

Q2) Yes the line breaks and I want the title to before the description and keywords.
0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 33656170
The function should be something like this:

    public void SetParameters(System.Web.UI.Page p)
    {
        p.Title = "TITLE";
        System.Web.UI.HtmlControls.HtmlMeta hm1 = new System.Web.UI.HtmlControls.HtmlMeta();
        System.Web.UI.HtmlControls.HtmlMeta hm2 = new System.Web.UI.HtmlControls.HtmlMeta();
        hm1.Name = "description";
        hm1.Content = MetaDescription;
        hm2.Name = "keywords";
        hm2.Content = MetaKeywords;
        System.Web.UI.HtmlControls.HtmlHead head = (System.Web.UI.HtmlControls.HtmlHead)p.Header;
        head.Controls.Add(hm1);
        head.Controls.Add(hm2);
    }

Open in new window

0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:bongii
ID: 33656177
How do I call the function?
System.Web.UI.Page p
SetParameters(???)

0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 33656178
Q2) To do that, I think it is a matter of ASP .NET's internal mechanisms, a workaround could be this, you implement something on your own. e.g. the page be some thing like this:

And you add a method named GetCustomTags to your code behind file, like the below code and in that function, you return tags needed to be placed after title tag.


    public string GetCustomTags()
    {
        return "return what ever tags you need";
    }

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <%= this.GetCustomTags() %>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    </form>
</body>
</html>

Open in new window

0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 33656180
About calling this function, you just need to call this in Page_Init event like this:

this.SetParameters(this);    

Open in new window

0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 33656182
Sorry, in the above code I assumed that you have placed SetParameters function inside the page class, if it is in an other class, you just need to declare a variable of that class type, initialize it and call the SetParameters function as shown earlier.
0
 
LVL 5

Expert Comment

by:pschrama
ID: 33656200
You could make a static function in a separate class. See below example. You can call the method from your page codebehind with HeaderHelper.SetHeader(this.Page, "page name", "page description", "page keywords").
public class HeaderHelper
    {
        public static void SetHeader(Page page, string name, string metaDescription, string metaKeywords)
        {
            page.Title = name;
            HtmlMeta hm1 = new HtmlMeta();
            HtmlMeta hm2 = new HtmlMeta();
            hm1.Name = "description";
            hm1.Content = metaDescription;
            hm2.Name = "keywords";
            hm2.Content = metaKeywords;
            HtmlHead head = (HtmlHead)page.Header;
            head.Controls.Add(hm1);
            head.Controls.Add(hm2);
        }
    }

Open in new window

0
 
LVL 5

Expert Comment

by:pschrama
ID: 33656203
To adjust the way the html is rendered, I found a blog post with a possible solution:

http://blogs.x2line.com/al/archive/2007/01/10/2773.aspx
0
 

Author Comment

by:bongii
ID: 33656212
pschrama: How does I call the class?
HeaderHelper.SetHeader(???, "name", "description", "Keywords"); ?
0
 
LVL 5

Accepted Solution

by:
pschrama earned 500 total points
ID: 33656246
You can call the method from a page's codebehind like this:

HeaderHelper.SetHeader(this.Page, "page name", "page description", "page keywords");
0
 

Author Comment

by:bongii
ID: 33656467
Thank you pschrama:. It works just the way I wants :)

now I just need to write the title in beginning of site and not below description and keyword. The article posted earlier in this thread didn´t help me
0
 
LVL 12

Expert Comment

by:Mortaza Doulaty
ID: 33657216
Have you seen my comment?
0
 

Author Comment

by:bongii
ID: 33659921
Mortaza_doulaty: Yes I have seen your comments. Thank you.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question