2014-09-22 94 views
0

我有一個具有本地化功能的網絡表單。我添加了西班牙語作爲webform的第二語言,並且完美地工作。但是,我發現這個問題,我只能用英語或西班牙語以一種語言啓動webform,但我想在運行時更改它。在運行時使用資源文件進行本地化

我想通過下拉框(無論是在運行時的英語或西班牙語)增加以下功能網絡表單

1)選擇語言,以進一步改善這一點。

2)顯示英語作爲第一,如果用戶想改變它,用戶應該從下拉框中選擇默認語言

是上述2提到的功能可以添加?如果是的話請說明一下。

這裏是我的網頁表單代碼:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="About_Company.aspx.cs" Inherits="AutoMobileWebsite.AboutUs" UICulture="es-ES"%> 

     <!DOCTYPE html> 

    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
    <title>Company</title> 
    </head> 
    <body> 
    <form id="form1" runat="server"> 

    <div> 
     <asp:Label id="lblAboutCM" runat="server" meta:resourcekey="lblAboutCM"><h1>About Company :</h1></asp:Label> 
</div> 

    <div> 
    <asp:Label id="lblContent" runat="server" meta:resourcekey="lblContent"> <h3> The company was established in 2014 by the founder Rex Chris. 
     The intention of this website is provide our customers and online virtual car sale which 
     anytype of customer can buy their dream vehicle or sell the existing vehicle 
     </h3> 
     </asp:Label> 
</div> 

    </form> 

</body> 

我如何實現上述功能?

謝謝您的時間

+0

看一看這個http://tutorialhouse.weebly.com/cnet/implement-全球化功能於ASPNET – Sam 2014-09-22 03:52:19

回答

1

ASPX:

<form id="form1" runat="server"> 
Language:<br /> 
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" 
    onselectedindexchanged="DropDownList1_SelectedIndexChanged"> 
    <asp:ListItem Value="English">English</asp:ListItem> 
    <asp:ListItem>Spanish</asp:ListItem> 
</asp:DropDownList> 
    <div> 
    <asp:Label id="eng_lblAboutCM" runat="server" meta:resourcekey="lblAboutCM"><h1>english about :</h1></asp:Label> 
    <asp:Label id="sp_lblAboutCM" runat="server" meta:resourcekey="lblAboutCM"> <h1>spanish about :</h1></asp:Label> 
</div> 
<div> 
<asp:Label id="eng_lblContent" runat="server" meta:resourcekey="lblContent"> <h3> english content 
    </h3> 
</asp:Label> 
    <asp:Label id="sp_lblContent" runat="server" meta:resourcekey="lblContent"> <h3> spanish content 
    </h3> 
    </asp:Label> 
    </div> 
    </form> 

後面的代碼:

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     ChangeLanguage(); 
    } 
    private void ChangeLanguage() 
    { 
     foreach (var item in form1.Controls) 
     { 
      if (item is Label) 
      { 
       Label lbl = (Label)item; 
       lbl.Visible = false; 
       if (lbl.ID.StartsWith("eng") && 
        DropDownList1.SelectedItem.Text == "English") 
       { 
        lbl.Visible = true; 
       } 
       else if (lbl.ID.StartsWith("sp") && 
        DropDownList1.SelectedItem.Text == "Spanish") 
       { 
        lbl.Visible = true; 
       } 
      } 
     } 
    } 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!IsPostBack) 
     { 
      ChangeLanguage(); 
     } 
    } 
相關問題