How to Show Your Meta Description and Keywords on an ASP Master Page

by Patrick 7. November 2008 14:11

So today's challenge was to display the Title, Meta description and meta tags on a site I am currently working on for troubleshooting and SEO reasons.

The solution I came up with was to place the following code in the code behind of the masterpage.


protected void Page_Load(object sender, EventArgs e)
        {
HtmlHead headTag = (HtmlHead)Page.Header;
            Response.Write("Title of Page: " + headTag.Title + "");

            foreach (var control in Page.Header.Controls)
            {
                var test = control.GetType();
                if (test.Name == "HtmlMeta")
                {
                    if ((control as HtmlMeta).Name == "Description")
                    {
                        Response.Write("MetaDescription : " + (control as HtmlMeta).Content + "");
                    }
                    if ((control as HtmlMeta).Name == "Keywords")
                    {
                        Response.Write("MetaKeywords : " + (control as HtmlMeta).Content + "");
                    }
                }
            }
}

The HeadTag.Title gives us the Title of the current page.
The foreach loop jumps through each control in the header section of the current page.
Since there can be different types of controls in the header section, we need to check for the type of HtmlMeta.
Then we need to check to see if it is the Description or Keywords and return the content values.

I am sure there are other ways, but this is a really simple way to display the 3 main SEO elements, Page Title, Page MetaTag Description and Page MetaTag Keywords for all the ASP.NET pages that use this master page (or you can place it on each page code behind) without having to view the source code.

Just remember to remove this code before you launch your site :)

WPF Dynamically Added Controls and Getting Values

by Patrick 4. November 2008 11:11

Today's challenge was fun.

I had added a bunch of textboxes and labels to a Grid with 2 StackPanel in my code behind.


StackPanel sp1 = new StackPanel();
StackPanel sp2 = new StackPanel();
Grid g = new Grid();
                ColumnDefinition colDef1 = new ColumnDefinition {Width = new GridLength(50)};
                ColumnDefinition colDef2 = new ColumnDefinition();
                g.ColumnDefinitions.Add(colDef1);
                g.ColumnDefinitions.Add(colDef2);
gb.Content = g;
                Grid.SetColumn(sp1,0);
                g.Children.Add(sp1);
                Grid.SetColumn(sp2, 1);
                g.Children.Add(sp2);

for (int i = 1, i < = 4, i++)
{
                Label lbl = new Label {Content = "Name", Margin = new Thickness(0)};
                Grid.SetColumn(lbl, 0);
                sp1.Children.Add(lbl);
                TextBox fieldname = new TextBox {Text = "", Name = "fieldname" + i, Margin = new Thickness(0,5,0,0)};
                Grid.SetColumn(fieldname, 1);
                sp2.Children.Add(fieldname);
}

So I then wanted to get the results of what someone types into these TextBoxs.

But wait, I can't just access them like this:


string t = fieldname1.Text;

Why? Only Microsoft knows, but apparently, dynamically added controls can't be accessed this way since they don't exist in the XAML code.

So, how does one access them then? Well, that's where this cool LogicalTreeHelper function to find the control by name we use the FindLogicalNode option of the LogicalTreeHelper function

So to loop through the number of fields you added you can do the following:


List test = new List;
for (int i = 1; i < = 4; i++)
{
test.Add((LogicalTreeHelper.FindLogicalNode(Fields1, "fieldname" + i) as TextBox).Text);
}

So there you have it, a simple way to access dynamically added controls using LogicalTreeHelper and FindLogicalNode just remember to name your controls you add.

Tags: ,

Coding | C# | WPF

Writing Out LINQ to XML Documents to CSV Text Files

by Patrick 1. November 2008 19:11

This may sound stupid, but I wanted to store some text parsing I was doing for some VDP projects in LINQ to XML XDocuments.

So, I created the following:

XDocument recs = new XDocument();
XElement records1 = new XElement("Records");
XElement record1 = new XElement("Record");
record1.Add(new XElement("id", "1"),
new XElement("Name","Test"),
new XElement("Address","123 Anywhere")
);
records1.Add(record1);
recs.Add(records1);

which should create an xml document looking like this:

<Records>
<Record>
<id>1</id>
<Name>Test</Name>
<Address>123 Anywhere</Address>
</Record>
</Records>

So, I want to write this out to a CSV file. But I want to assume that my XML Nodes might change (but at least stay consistent throughout the XML Document) by datasource and I want to systematically write out the names of the nodes and then loop through and write out the values.

So the hard part:

var list2 = from el2 in recs.Elements("Record")
select el2;
//Build Header Line
StringBuilder csvlist = new StringBuilder();
var headers = (list2.First().Nodes().ToList());
int cc = 1;
foreach (var node in headers)
{
csvlist.Append((node as XElement).Name);
if (cc != headers.Count)
{
csvlist.Append(",");
}
cc++;
}
csvlist.AppendLine();
foreach (var t in list2)
{
for (int i = 0; i < t.Nodes().Count(); i++)
{
csvlist.Append((t.Nodes().ToList()[i] as XElement).Value);
if (i != (t.Nodes().Count() - 1))
{
csvlist.Append(",");
}
}
csvlist.AppendLine();
}

So at the end of all this, csvlist contains the headers and data as comma separated.

There are lots we could add or fix in this, but it allows us great flexibility in outputting xml data as delimited text.

DISCLAIMER: I am not a professional coder, I do this as a passion. The above code has no warranty or ability to actually do what I say it does. Use at your own risk, fix, add or change if you are willing.

Tags:

Coding | C# | CSV | LINQ

Microsoft C#, LINQ, XML, WPF and the Lack of Good Examples

by Patrick 1. November 2008 19:11

Ok,

My rant for the day is about trying to use Microsoft C# and LINQ and XML and WPF. I spend more time trying to figure out simple stuff, because nothing exists as solid examples of how to do what I need to do.
I spend so much of my time trying to find decent examples of how to do stuff on the net, and find nearly nothing.
Why?
I know people are doing this stuff, is it because its a closed source system and sharing the code just isn't "cool" by the users?
I am sick of it, and I am going to try to lead by example and post my minor victories in trying to do things using this great platform.

Why?
Well, couple of reasons...
1. Code should be free. Not as in not get paid for it, but how to do something should be shared, knowledge isn't worth anything unless you share it.
2. Selfish reasons, I can find different things I find much easier and watch my progression through the products
3. Cause I haven't really found a good reason to keep posting regularly on my blog, and this just might be a good excuse.

So, there you have it, my rant for the day and my promise to share my code examples of what I am trying to do.

Tags:

Coding | ASP.NET | C# | LINQ | WPF | XML | Rants | Software

Powered by BlogEngine.NET 1.6.0.0

 

Call Me

RecentComments

Comment RSS