2011-07-20 31 views
0

我有一個主頁,其中定義了元標記名稱和描述。如果我這樣做,我無法使頭部使用runat="server",因爲我在使用URL路由時遇到了一些問題。動態更改元標記時出現錯誤

我想要動態更改網站中某個子頁面的元描述。我曾經嘗試這樣做:

HtmlHead headTag = (HtmlHead)this.Header; 
HtmlMeta pageMetaTag = new HtmlMeta(); 
pageMetaTag.Name = "Description"; 
pageMetaTag.Content = "Test"; 
headTag.Controls.Add(pageMetaTag); 

但我上添加行的錯誤,他說:

Object reference not set to an instance of an object. 

了HEAD內容保持<head runat="server"></head>爲子頁面,該頁面運行,但meta描述標籤未被覆蓋。 meta描述相同的母版頁的... 請幫助我解決這個問題....

使用:ASP.NET 4 VS2010

更新(子頁面看起來很相像):

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="Profile.aspx.cs" Inherits="Profile"%> 
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="ajax" %> 

<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" Runat="Server"> 
    <head runat="server"></head> 
    <style type="text/css" runat="server"> 
        .lnkbtn 
        { 
         color: #000080; 
         cursor:pointer; 
        } 
        .lnkbtn:hover 
        { 
         color: #800000; 
         text-decoration: underline; 
        } 
    </style> 
<link rel="stylesheet" type="text/css" href='<%= ResolveUrl("~/css/back.css") %>'/> 
    <script type="text/javascript" src='<%= ResolveUrl("~/css/front.js") %>'></script> 
</asp:Content> 
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" Runat="Server"> 
    // contains 
    // update panel 
    // webpartmanager 
    // webpartzones 
</asp:Content> 

更新(頭嵌套頭標籤內得到)

在使用Page.MetaDescription = "Hi How are you";以下是頁面的源代碼:

<head> 
    <titleMaster Page Title</title> 
    <meta name="Keywords" content="Master page keywords" /> 
    <meta name="description" content="Master page meta description." /> 
    . 
    . 
    . 
    <head><title>Child page Title</title> 
    <meta name="description" content="Hi How are you" /></head> 

    . 
    . 
    . 
</head> 
+0

什麼asp.net版本您使用的? – Pleun

+0

@Pleun在VS2010中的ASP.NET 4 – Jayesh

+0

好的,在這種情況下應該很容易,看到我的答案在下面 – Pleun

回答

0

試試這個

HtmlMeta keywords = new HtmlMeta(); 
keywords.Name = "keywords"; 
keywords.Content = "google, yahoo"; 
Header.Controls.Add(keywords); 

確保您標記

<head runat="server"> 
+0

不起作用。檢查編輯後的問題。 – Jayesh

0

我想你需要

<head runat="server"> 

Header.Controls.Add(pageMetaTag); 

以及至少我使用這個確切的代碼和它的作品

 HtmlMeta siteVerificationMeta = new HtmlMeta(); 
     siteVerificationMeta.Name = "xxxx"; 
     siteVerificationMeta.Content = "xxxx"; 
     Header.Controls.AddAt(0, siteVerificationMeta); 

編輯:我覺得這是你需要的,要添加一個新的標題是什麼,你需要下面編輯

請注意註釋掉的線!

 //HtmlHead headTag = (HtmlHead)this.Header; 
    HtmlMeta pageMetaTag = new HtmlMeta(); 
    pageMetaTag.Name = "Description"; 
    pageMetaTag.Content = "Test"; 
    Header.Controls.Add(pageMetaTag); 
+0

如果我允許runat服務器在子頁面的頭部內容中,即使此時顯示母版頁的描述... – Jayesh

+0

我可以看到子頁面的樣子嗎? – inspite

+0

檢查更新後的問題。 – Jayesh

0

在Asp.net 4.0,你可以這樣做:

protected void Page_Load(object sender, EventArgs e) 
{ 

    Page.MetaDescription = "My page - a great page indeed"; 
    Page.MetaKeywords = "keyword1, two, three"; 
    } 
+0

沒有工作。說明與母版頁的說明相同。讓我明確指出,母版頁包含關鍵字,以aspx定義的描述標籤,並且頭標記中沒有runat =「server」。我不能讓它runat服務器因爲我面臨的URL路由問題。在子頁面中存在。問題是頭被嵌套在頭標籤下。我有兩個頭標籤在另一個裏面。外部的是所採取的母版頁。內部的是被拒絕的子頁面。 – Jayesh