2017-01-10 31 views
2

我需要將從第一頁選擇的全球化資源語言傳遞到下一頁。請按照圖片和代碼段,告訴我它有什麼問題。如何將資源語言傳遞到其他頁面

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)請參考附件中的資源文件。我已經完成了這些步驟。但它不工作。請幫幫我 。 :-)

enter image description here enter image description here

回答

0

我明白我的錯誤。上面的代碼工作正常,它也將語言傳遞到下一頁。問題是我正在使用本地資源,我只爲1頁添加了resx文件。應該將resx文件添加到兩個頁面以獲得預期的結果。

0

您可以在查詢字符串傳遞到其他頁面:

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

然後在BasePage讀出的值,並設置屬性:

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