Dynamically writing your META tags using ASP.NET C#
1: protected void Page_Load(object sender, EventArgs e)
2: {
3: //Add copyright header with dynamic copyright year
4: WriteMetaTag(this, "copyright", "©, Copyright " + System.DateTime.Today.Year.ToString() + " Lifetime Products. All Rights Reserved.");
5: }
6:
7: /// <summary>
8: /// Static method that writes a given HtmlMeta tag with the supplied parameters
9: /// </summary>
10: /// <param name="ctrl">the control ("this" will suffice)</param>
11: /// <param name="tag">the meta-tag to be written (title, keyword, description, etc.)</param>
12: /// <param name="value">the value of the meta-tag (the page's title, a keyword list, the page's description, etc.)</param>
13: /// <returns>
14: /// A properly formatted HTML or xHTML meta-tag value-pair written into the page header
15: /// </returns>
16: public static void WriteMetaTag(Control ctrl, string tag, string value)
17: {
18: Page p = (Page)ctrl;
19: HtmlMeta m = new HtmlMeta();
20: m.Name = tag.ToLower();
21: m.Content = value;
22: p.Header.Controls.Add(m);
23: }
One less place you need to babysit the date.
Posted in ASP.NET, Experimentation, Internet, Joe, Lifetime Products, Web Development, technology, work



January 15th, 2008 at 14:08
Also, don’t forget to add your “using System.Web.UI.HtmlControls;” statement, or fully qualify your HtmlMeta m = … line to reflect the right library.
- http://www.JoeLevi.com