我现在做了一个类里面写的是
public class initfunc : System.Web.UI.Page
{
public initfunc()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public void setMeta(string key, string des)
{
HtmlMeta keywords = new HtmlMeta(), description = new HtmlMeta(); // keywords, description
keywords.Name = "keywords";
keywords.Content = key;
description.Name = "description";
description.Content = des;
Page.Header.Controls.Add(keywords);
Page.Header.Controls.Add(description);
}
}
然后我在test.aspx.cs代码中这么写
initfunc ini = new initfunc();
protected void Page_Load(object sender, EventArgs e)
{
ini.setMeta("1111", "2222");
}
程序出现错误:System.NullReferenceException: 未将对象引用设置到对象的实例。
我就是想在编写每个页面代码的时候都能调用这个类进行修改 keywords description。希望帮我解答或者帮我想个更好的办法!
------解决方案--------------------------------------------------------
- C# code
// Render: <meta name="keywords" content="Some words listed here" />HtmlMeta meta = new HtmlMeta();meta.Name = "keywords";meta.Content = "Some words listed here";this.Header.Controls.Add(meta);// Render: <meta name="robots" content="noindex" />meta = new HtmlMeta();meta.Name = "robots";meta.Content = "noindex";this.Header.Controls.Add(meta);// Render: <meta name="date" content="2006-03-25" scheme="YYYY-MM-DD" />meta = new HtmlMeta();meta.Name = "date";meta.Content = DateTime.Now.ToString("yyyy-MM-dd");meta.Scheme = "YYYY-MM-DD";this.Header.Controls.Add(meta);
------解决方案--------------------------------------------------------
我是写在masterpage里,不同的页,赋值Session("currentpage"),根据库里的不同页不同关键词填充。
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
makemeta(Session("currentpage"))
End Sub
Sub makemeta(ByVal item As String)
Dim ds As DataSet = toolClass.getmeta(item)
Page.Title = ds.Tables(0).Rows(0)("title")
Dim meta As New HtmlMeta
meta.Name = "keywords"
meta.Content = ds.Tables(0).Rows(0)("keywords")
Page.Header.Controls.Add(meta)
meta.Name = "description"
meta.Content = ds.Tables(0).Rows(0)("description")
Page.Header.Controls.Add(meta)
End Sub
------解决方案--------------------------------------------------------
- C# code
public class initfunc : System.Web.UI.Page{public initfunc(){//// TODO: 在此处添加构造函数逻辑//} public void setMeta(System.Web.UI.Control page,string key, string des) { HtmlMeta keywords = new HtmlMeta(), description = new HtmlMeta(); // keywords, description keywords.Name = "keywords"; keywords.Content = key; description.Name = "description"; description.Content = des; page.Header.Controls.Add(keywords); page.Header.Controls.Add(description); }} //加个参数,使用的时候initfunc ini = new initfunc();protected void Page_Load(object sender, EventArgs e){ ini.setMeta(this,"1111", "2222");}
------解决方案--------------------------------------------------------
- C# code
public class AA { public static void setMeta(string keyWords) { System.Web.UI.HtmlControls.HtmlMeta m = new HtmlMeta(); m.Name = "keywords"; m.Content = keyWords; System.Web.UI.Page p = (System.Web.UI.Page)System.Web.HttpContext.Current.Handler; p.Header.Controls.Add(m); } }调用:protected void Page_Load(object sender, EventArgs e) { AA.setMeta("关键词AAA"); }
------解决方案--------------------------------------------------------
幕白兄来了!!