【问题标题】:How to pass the resource language to a different page如何将资源语言传递到不同的页面
【发布时间】:2017-01-10 04:10:49
【问题描述】:

我需要将从第一页选择的全球化资源语言传递到下一页。请按照图像和代码段告诉我它有什么问题。

1) 我正在使用以下代码从下拉菜单中获取语言

protected void Button1_Click(object sender, EventArgs e)
{
    BasePage.CultureName = DropDownList1.SelectedItem.Value.ToString();
    Response.Redirect("Page1.aspx");
}

2) 传递给这个函数

public class BasePage : System.Web.UI.Page
{
    public BasePage()
    {     
    }

    static string cultureName;
    public static string CultureName
    { 
        return cultureName; 
    }
    set 
    { 
        cultureName = value; 
    }
}

protected override void InitializeCulture()
{
    Thread.CurrentThread.CurrentCulture =
    CultureInfo.CreateSpecificCulture(cultureName);
    Thread.CurrentThread.CurrentUICulture = new
    CultureInfo(cultureName);
    base.InitializeCulture();
}

3) 传递的变量是通过使用 BasePage 类继承来从这里读取的

public partial class Page1 : BasePage
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

4) ASPX 文件

<pre>
<%@ Page Language="C#" AutoEventWireup="true"CodeBehind="Page1.aspx.cs" 
meta:resourcekey="PageResource1"  Inherits="Globalization.Page1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
    </head>
    <body>
        <form id="form1" runat="server">
           <div>
               <asp:Label ID="Label2" runat="server"
meta:resourcekey="Label2Resource1" />
           </div>
                <asp:Button ID="Button1" runat="server" 
meta:resourcekey="ButtonResource1"/>
        </form>
    </body>
</html>
</pre>

5) 资源文件请参考附件图片。我已经完成了这些步骤。但它不起作用。请帮我 。 :-)

【问题讨论】:

    标签: c# asp.net globalization culture asp.net-core-localization


    【解决方案1】:

    您可以在查询字符串中将其传递给其他页面:

    BasePage.CultureName = DropDownList1.SelectedItem.Value.ToString();
    Response.Redirect(string.Format("Page1.aspx?culture={0}", BasePage.CultureName);
    

    然后在BasePage中读取值并设置属性:

    public BasePage()
    {  
        this.CultureName = Request.QueryString["culture"];   
    }
    

    【讨论】:

      【解决方案2】:

      我理解我的错误。上面的代码工作正常,它也将语言传递到下一页。问题是我使用的是本地资源,我只为 1 页添加了 resx 文件。应将 resx 文件添加到两个页面中以获得预期的结果。

      【讨论】:

        猜你喜欢
        • 2018-10-07
        • 2015-11-13
        • 2019-02-01
        • 1970-01-01
        • 2019-07-19
        • 2019-03-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多