2011-12-27 46 views
0

有沒有一種方法可以從InitializeCulture訪問頭標籤,因此我可以設置頭標籤lang屬性。我得到的對象引用不設置到對象InitializeCulture對象引用中的頭標籤而不是對象的實例

protected override void InitializeCulture() 
{ 
    if (Request[PostBackEventTarget] != null) 
    { 
     string controlID = Request[PostBackEventTarget]; 
     // Request.Form[Request[PostBackEventTarget]].ToString(); 
     string selectedValue = Request.Form[LanguageDropDownID].ToString(); 
     if (controlID.Equals(Request.Params.Get("__EVENTTARGET"))) 
     { 
      Thread.CurrentThread.CurrentCulture = new CultureInfo(selectedValue); 
      Thread.CurrentThread.CurrentUICulture = new CultureInfo(selectedValue); 
      Page.Header.Attributes.Add("lang", selectedValue); // error 
     } 
    } 
    else 
    { 
     string culture = (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture; 
     Thread.CurrentThread.CurrentCulture = new CultureInfo(culture); 
     Thread.CurrentThread.CurrentUICulture = new CultureInfo(culture); 
     HtmlHead header = Page.Header as HtmlHead; 
     header.Attributes.Add("lang", culture); // error 
    } 
    base.InitializeCulture(); 
} 

問題是訪問head標籤和屬性添加到它 和實例也爲什麼我的頭標記輸出不同的HTML標籤如

<html lang="<%= (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture %>" xmlns="http://www.w3.org/1999/xhtml"> 
<head lang='<%= (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture %>' runat="server"> 
存在

輸出

<html lang="fa-IR" xmlns="http://www.w3.org/1999/xhtml"> 
<head lang="&lt;%= (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture %>"> 
+1

也許你的一小段源代碼會給一些東西詳細說明 – rene 2011-12-27 21:26:41

回答

0

來解決問題的幾種方法。

首先,直接回答你的問題。在服務器標記中使用<%#,包括帶有runat="server"的HTML標記。 <%=僅用於純HTML標記。

<head lang='<%# (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture %>' runat="server"> 

其次,關於InitializeCulture設置屬性......我相信InitializeCulture是設置當前文化的要求,而不是爲標記。嘗試以後的活動,例如OnInitOnLoad

最後,也許最重要的是,HEAD標記不需要lang屬性,除非與HTML標記不同。 lang從其父項繼承。只有HTML標記需要lang屬性。

相關問題