ASP的可本地化下拉列表。NET 2.0

下载源码- 10.88 KB 介绍 代码展示了一种简单的绑定基于ListItem的控件的方法,比如下拉菜单到一个资源文件。 Background  其思想是将数据存储为XML格式,而不是名称值集合,并使用XMLDataSource对象将其绑定到。net控件。, 使用Code , 我添加了一个字符串资源“MyCountryList”,

  

 

它包含了用于下拉控件的XML数据。, Resource.en-US.resx 隐藏,复制Code

<countries>
  <countrytext="Pakistan"value="PK"/>
  <countrytext="Iran"value="IR"/>
  <countrytext="Afghanistan"value="AF"/>
</countries>

Resource.es-MX.resx 隐藏,复制Code

<countries>
  <countrytext="Pakistan-Spanish"value="PK"/>
  <countrytext="Iran-Spanish"value="IR"/>
  <countrytext="Afghanistan-Spanish"value="AF"/>
</countries>

default . aspx 隐藏,复制Code

<formid="form1"runat="server">
        <asp:HyperLinkID="HyperLink1"runat="server"NavigateUrl="~/Default.aspx?lang=esp">Esp</asp:HyperLink>
          
        <asp:HyperLinkID="HyperLink2"runat="server"NavigateUrl="~/Default.aspx?lang=eng">Eng</asp:HyperLink><br/>
        <br/>
        <asp:LocalizeID="Localize1"runat="server"Text="<%$ Resources:Resource, Greeting%>"></asp:Localize>
        <br/>
        <asp:XmlDataSourceID="XmlDataSource1"runat="server"></asp:XmlDataSource>
        <asp:DropDownListID="DropDownList1"runat="server">
        </asp:DropDownList>
</form>

Default.aspx.cs 隐藏,收缩,复制Code

protected void Page_Load(object sender, EventArgs e)
    {
        // loading xml from resource file into xmldatasource object
        XmlDataSource1.Data = Resources.Resource.MyCountryList;
        XmlDataSource1.EnableCaching = false;
        XmlDataSource1.DataBind();        

        DropDownList1.DataSourceID = "XmlDataSource1";
        DropDownList1.DataTextField = "text";
        DropDownList1.DataValueField = "value";
        DropDownList1.DataBind();
    }
   //The setting up the culture
    protected override void InitializeCulture()
    {
        string lang = "en-US";
        if (Request.QueryString["lang"] != null)
        {
            switch (Request.QueryString["lang"])
            {
                case "esp":
                    lang = "es-MX";
                    break;
                case "eng":
                    lang = "en-US";
                    break;
            }
        }
        System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(lang);
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
        base.InitializeCulture();
    } 

斜体显示了绑定到资源文件的两种方法。您还可以下载本文附带的源代码。 历史 2009年1月1日:初任 本文转载于:http://www.diyabc.com/frontweb/news330.html

posted @ 2020-08-05 08:28  Dincat  阅读(154)  评论(0编辑  收藏  举报