How do I read a meta tag of a content page from codebehind

I have the following markup in my content page

<%@ Page Title="" Language="C#" MasterPageFile="~/Layout/Bootstrap.Master" AutoEventWireup="true" CodeBehind="Charts.aspx.cs" Inherits="Dashboard.Charts"  %>

<%@ MasterType VirtualPath="~/Layout/Bootstrap.Master" %>

<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeadContent" runat="server">
    <meta name="Category" content="Dashboard" />
    <meta name="Roles" content="Sales,Marketing,Outside Agent" />

Open in new window

I'm trying to figure out how I can find the meta tags from the codebehind. I can't seem to see the HeaderContent object and the following code only returns the Meta tags from the masterpage and not from the content page.
            foreach (object control in this.Header.Controls)
            {
                System.Web.UI.HtmlControls.HtmlMeta metaTag = control as System.Web.UI.HtmlControls.HtmlMeta;
                if (metaTag != null)
                {
                    System.Diagnostics.Debug.Print(metaTag.Name);
                }
            }

Open in new window

Can anyone tell me how I would go about finding the meta tag of the content page?
LVL 21
Russ SuterAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
from:
http://forums.asp.net/t/1455331.aspx?How+to+read+Meta+tags+in+C+
 foreach (IHTMLElement el in (IHTMLElementCollection)doc.all)
        {
            // check to see if all the desired attributes were found with the correct values
            bool qualify = true;
            if (el.tagName == "META")
            {
                HTMLMetaElement meta = (HTMLMetaElement)el;
                Response.Write("Content " + meta.content +"<br/>");
            }
          
        }

Open in new window


Note they mention an agility pack which can also help you:
http://htmlagilitypack.codeplex.com/
0
Russ SuterAuthor Commented:
That doesn't address my question. The code you provided reads meta tags from an outside HTML page. I need to read the meta tags from the content page in the Page_Load event handler. All I can currently see is the meta tags from the master page.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
//assuming Page != PostBack

            HtmlHead pHtml = Page.Header;

            foreach (HtmlMeta metaTag in pHtml.Controls.OfType<HtmlMeta>())
            {
              // do something             
            }

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Russ SuterAuthor Commented:
That gives me a compiler error

'System.Web.UI.ControlCollection' does not contain a definition for 'OfType' and no extension method 'OfType' accepting a first argument of type 'System.Web.UI.ControlCollection' could be found (are you missing a using directive or an assembly reference?)
0
Kyle AbrahamsSenior .Net DeveloperCommented:
What version of .Net you using?
0
Russ SuterAuthor Commented:
4.5
0
Kyle AbrahamsSenior .Net DeveloperCommented:
do you have the using System.Linq;  

That code compiles fine for me.
0
Russ SuterAuthor Commented:
Yes, System.Linq is included.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
Does your code look like this (including colors?)  I'm wondering if there's something else missing?   Also where are you putting that code?

Example
0
Russ SuterAuthor Commented:
No, it looks like this...
code screenshotand I've tried putting it in the OnInit and the Page_Load methods.
0
Russ SuterAuthor Commented:
I've opted for a workaround to this problem. By inserting the values I need in the MetaDescription attribute of the page header I'm able to read the "Description" meta tag which is always present on the master page. It's not my ideal solution but it does work.

<%@ Page Title="" Language="C#" MasterPageFile="~/Layout/Bootstrap.Master" AutoEventWireup="true" CodeBehind="Charts.aspx.cs" Inherits="UI.Dashboard.Charts" MetaDescription="foo"  %>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Kyle AbrahamsSenior .Net DeveloperCommented:
You're missing the parentheses "()" at the end of the OfType:


            foreach (HtmlMeta metaTag in pHtml.Controls.OfType<HtmlMeta>())
            {
              // do something            
            }

The solution does work.
0
Russ SuterAuthor Commented:
That fixed the compiler error but it still only sees the meta tags of the master page so it still doesn't address my original question. The workaround I posted previously is just going to have to suffice.
0
Russ SuterAuthor Commented:
Solutions previously posted simply didn't work. While this is not ideal it does solve my immediate problem.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.