2011-12-27 12 views
0

InitializeCultureからヘッドタグにアクセスする方法がありますので、headタグ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

答えて

0

いくつかの方法が存在します。

まず、質問への直接的な回答。 runat="server"のHTMLタグを含め、サーバータグには<%#を使用してください。 <%=は、プレーンなHTMLマークアップのためだけです。 InitializeCultureに属性を設定に関する

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

第二には、...私はInitializeCultureはないマークアップのために、要求のための現在のカルチャを設定するためのものであると信じています。後のイベント(OnInitまたはOnLoadなど)を試してください。

最後に、最も重要なことに、HEADタグには、HTMLタグと異なる場合を除き、lang属性は必要ありません。 langは親から継承されています。 HTMLタグのみがlang属性を必要とします。

関連する問題