Solved

Meta info from codebehind

Posted on 2010-09-12
14
618 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 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