InitializeCulture对象引用中的访问头标记不是对象的实例
本文关键字:对象 实例 对象引用 访问 InitializeCulture | 更新日期: 2023-09-27 18:18:57
是否有办法从InitializeCulture访问头标记,以便我可以设置头标记lang属性。我得到的对象引用没有设置为对象的实例
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="<%= (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture %>">
有几种方法可以解决这个问题。
首先,直接回答你的问题。在服务器标记中使用<%#
,包括带有runat="server"
的HTML标记。<%=
仅用于普通HTML标记。
<head lang='<%# (HttpContext.Current.Profile as ProfileCommon).Preferences.Culture %>' runat="server">
第二,关于在InitializeCulture
中设置属性…我相信InitializeCulture
是为请求设置当前文化,而不是标记。请尝试稍后的事件,例如OnInit
或OnLoad
。
最后,也许也是最重要的,除非与HTML标记不同,否则HEAD标记不需要lang
属性。lang
是从它的父节点继承的。只有HTML标签需要lang
属性