Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Meta info from codebehind

Posted on 2010-09-12
14
Medium Priority
?
623 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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 …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses

876 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