Solved

Meta info from codebehind

Posted on 2010-09-12
14
609 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
  • 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
 

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

760 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now