Although this is an old thread, I decided to use the LINQ-based approach to select the HtmlMeta control from the page control collection:
HtmlMeta htmlMetaCtrl = (from ctrls in page.Header.Controls.OfType<HtmlMeta>() where ctrls.Name.Equals("keywords", StringComparison.CurrentCultureIgnoreCase) select ctrls).FirstOrDefault(); if (htmlMetaCtrl != null) htmlMetaCtrl.Content = metaContent;
To change the meta tag dynamically, you can use the following general fuction:
public class WebUtils { public static void SetPageMeta(string metaName, string metaContent, HttpContext httpContext = null) { if (string.IsNullOrWhiteSpace(metaName)) return; if (metaContent == null) throw new Exception("Dynamic Meta tag content can not be null. Pl pass a valid meta tag content"); if (httpContext == null) httpContext = HttpContext.Current; Page page = httpContext.Handler as Page; if (page != null) { HtmlMeta htmlMetaCtrl = (from ctrls in page.Header.Controls.OfType<HtmlMeta>() where ctrls.Name.Equals(metaName, StringComparison.CurrentCultureIgnoreCase) select ctrls).FirstOrDefault(); if (htmlMetaCtrl != null) page.Header.Controls.Remove(htmlMetaCtrl); htmlMetaCtrl = new HtmlMeta(); htmlMetaCtrl.HttpEquiv = metaName; htmlMetaCtrl.Name = metaName; htmlMetaCtrl.Content = metaContent; page.Header.Controls.Add(htmlMetaCtrl); } else { throw new Exception("Web page handler context could not be obtained"); } } }
This can be called from OnPreRender from user controls (ascx controls) or Page_PreRender from a page (aspx):
protected override void OnPreRender(EventArgs e) { if (!IsPostBack) { WebUtils.SetPageMeta("keywords", "Your keyword, keyword2, keyword3"); WebUtils.SetPageMeta("description", "Your page description goes here..."); } base.OnPreRender(e); }
Hope this helps someone.
source share