【发布时间】:2011-10-05 05:17:44
【问题描述】:
我有一个 .NET 4.0 上的 ASP.NET 网站应用程序。有一个 Masterpage 包含所有 aspx 页面的页眉和页脚。内容来自各个 aspx 页面。我有所有 aspx 页面都继承自的 BasePage.cs。
现在解决问题: 我在母版页上有一个 HTML Select 控件,我试图使用下面的代码在 BasePage.cs 中检索其值
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
我在 InitializeCulture 方法中使用它,它将设置网站的文化信息。
protected override void InitializeCulture()
{
string language = ((System.Web.UI.HtmlControls.HtmlSelect)Master.FindControl("cmbLanguage")).Value;
Thread.CurrentThread.CurrentCulture =
CultureInfo.CreateSpecificCulture(language);
Thread.CurrentThread.CurrentUICulture = new
CultureInfo(language);
base.InitializeCulture();
}
在调试时,我可以看到 language 变量中设置了预期值。问题是当页面呈现时,aspx 页面的 ContentPlaceHolder 内的内容没有被呈现。
我可以看到是涉及FindControl 的代码是原因,因为如果我将语言设置为字符串,一切都会按预期工作。
string language = "de-DE";
我做错了什么?
更新: 如果 MasterPage 上的 ContentPlaceHolder 上有一些内容,则会呈现它而不是页面 ContentPlaceHolder。
【问题讨论】:
-
MasterPage的ContentPlaceHolder中是否有任何内容(标记)?不会呈现 contentplaceholder 控件中的标记。
-
@AVD:MasterPage 的 ContentPlaceHolder 中没有内容。
-
忘掉母版页吧,在调用 InitializeCulture 时您无法访问任何控件,因为它们根本不存在。您需要遵循答案中建议的解决方案:cookie,查询字符串值+会话或仅查询字符串值,url重写(www.mysite.com/en/products)
标签: asp.net master-pages findcontrol contentplaceholder