从UserControl for Globalization中的DropDownList获取选定值

时间:2013-08-26 19:30:54

标签: c# .net drop-down-menu localization globalization

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

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

我有一个下拉列表,可以为应用选择用户定义的语言。

Page.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;

    }

//等...

我最初使用这些文章: 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,但我仍然无法使用主模板。

0 个答案:

没有答案
相关问题