0

我需要获取下拉列表的选定值来更改应用程序的语言,但我得到一个对象引用未设置为从 Request.Form 读取的实例错误。

Page.ascx 中的下拉列表设置语言。哪件事通过继承 BasePage 类的 UserPage 类。

我有一个下拉列表来选择应用程序的用户定义语言。

页面.ascx

<asp:DropDownList ID="ddlLanguage" runat="server" AutoPostBack="True">
           <asp:ListItem Value="en-US">English</asp:ListItem>
           <asp:ListItem Value="fr-CA">French</asp:ListItem>
           <asp:ListItem Value="es-MX">Spanish</asp:ListItem>
</asp:DropDownList>

问题在这里:

 string selectedValue = Request.Form[Request[PostBackEventTarget]].ToString();

BasePage.cs 公共类 BasePage : System.Web.UI.Page {

    public const string PostBackEventTarget = "__EVENTTARGET";

    protected override void InitializeCulture()
    {

        if (Request[PostBackEventTarget] != null)
        {
            string controlID = Request[PostBackEventTarget];

            //split to get friendly ID
            string[] ddlcontrol = controlID.Split('$');


            if (ddlcontrol[1] == "ddlLanguage")
            {
                // the control will appear as _ctl4%24ddlLanguage or _ctl9%24ddlLanguage, etc.

                //Request.Form has the right name inside __EVENTTARGET
                // but the ToString bombs
                string selectedValue = Request.Form[Request[PostBackEventTarget]].ToString();

                SetCulture(selectedValue, selectedValue);

            }
        }
        if (Session["MyUICulture"] != null && Session["MyCulture"] != null)
        {
            Thread.CurrentThread.CurrentUICulture = (CultureInfo)Session["MyUICulture"];
            Thread.CurrentThread.CurrentCulture = (CultureInfo)Session["MyCulture"];
        }
        base.InitializeCulture();
    }

    protected void SetCulture(string name, string locale)
    {
        Thread.CurrentThread.CurrentUICulture = new CultureInfo(name);
        Thread.CurrentThread.CurrentCulture = new CultureInfo(locale);
        Session["MyUICulture"] = Thread.CurrentThread.CurrentUICulture;
        Session["MyCulture"] = Thread.CurrentThread.CurrentCulture;

    }

//ETC...

我最初使用这些文章开始:http: //www.codeproject.com/Articles/15313/Globalization-and-localization-demystified-in-ASP http://msdn.microsoft.com/en-us/library/bz9tc508 .aspx

该应用程序是从版本 1.0 更新到 4.0,但我仍然无法使用主模板。

4

0 回答 0