C# Adding MetaData to Master page from Child page dynamically
asp.net webform 의 child page 에서 master page 에 접근하여 metadata tag 값 추가하는 방법.
Masterpage.master.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class MasterPage : System.Web.UI.MasterPage
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreRender(EventArgs e)
{
if (Description != null)
{
HtmlMeta metaD = new HtmlMeta();
metaD.Name = "description";
metaD.Content = Description;
this.Page.Header.Controls.Add(metaD);
}
if (Keywords != null)
{
HtmlMeta metaK = new HtmlMeta();
metaK.Name = "description";
metaK.Content = Keywords;
this.Page.Header.Controls.Add(metaK);
}
base.OnPreRender(e);
}
public string Description
{
get
{
if (ViewState["MetaDescription"] != null)
return (string)ViewState["MetaDescription"];
else
return null;
}
set
{
ViewState["MetaDescription"] = value;
}
}
public string Keywords
{
get
{
if (ViewState["MetaKeywords"] != null)
return (string)ViewState["MetaKeywords"];
else
return null;
}
set
{
ViewState["MetaKeywords"] = value;
}
}
}
child page
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
(Master as MasterPage).Keywords = "sarin, mobi";
(Master as MasterPage).Description = "Sarin Information Technology Expert";
}
}
http://www.sarin.mobi/2008/10/c-adding-metadata-to-master-page-from-child-page-dynamically/