Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Meta info from codebehind

Posted on 2010-09-12
14
Medium Priority
?
621 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
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 

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

Certified OpenStack Administrator Course

We just refreshed our COA course based on the Newton exam.  With 14 labs, this course goes over the different OpenStack services that are part of the certification: Dashboard, Identity Service, Image Service, Networking, Compute, Object Storage, Block Storage, and Orchestration.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

721 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