2013-12-18 64 views
0

我正在尝试一些多语言网站,我现在在Dictionary对象中有价值,现在我想根据我的语言选择设置这些文本。下面是 是我的代码。如何在控制内设置数值

的Login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LoginForm.aspx.cs" Inherits="MultiligualApplication.LoginForm" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
    <% 
     System.Collections.Generic.Dictionary<string, string> dict = new System.Collections.Generic.Dictionary<string, string>(); 
     string className = "ClassEnLang"; 
     if (Session["lang"] != null) 
     { 
      if (Session["lang"].ToString().Equals("fr-FR")) 
      { 
       className = "ClassFrLang"; 
      } 
     } 

     if (className.Equals("ClassEnLang")) 
      dict = MultiligualApplication.ClassEnLang.dictionary; 
     else if (className.ToString().Equals("ClassFrLang")) 
     { 
      dict = MultiligualApplication.ClassFrLang.dictionary; 
     } 
    %> 


     <table style="width:100%;"> 
      <tr> 
       <td colspan="3"> 
        <asp:DropDownList ID="ddlLanguage" runat="server" 
         onselectedindexchanged="ddlLanguage_SelectedIndexChanged" 
         AutoPostBack="True"> 
         <asp:ListItem Value="en-US">English</asp:ListItem> 
         <asp:ListItem Value="fr-FR">French</asp:ListItem> 
        </asp:DropDownList> 
       </td> 
      </tr> 
      <tr> 
       <td colspan="3"><%=dict["loginhere"] %></td> 
      </tr> 
      <tr> 
       <td><%=dict["userid"] %></td> 
       <td> 
        <input id="txtUserId" type="text" runat="server" placeholder="<%= dict['userid']%>"/></td> 
       <td> 
        </td> 
      </tr> 
      <tr> 
       <td><%=dict["password"]%></td> 
       <td> 
        <input id="txtPassword" type="text" runat="server" placeholder="<%= dict['password']%>"/></td> 
       <td> 
        </td> 
      </tr> 
      <tr> 
       <td colspan="3"> 
        <asp:Label ID="lblErrorMsg" runat="server" Text=""></asp:Label> 
        </td> 
      </tr> 
      <tr> 
       <td> 
        </td> 
       <td> 
        <input id="btnLogin" type="submit" value="<%= dict["submit"]%>" runat="server"/></td> 
       <td> 
        &nbsp;</td> 
      </tr> 
     </table> 

    </div> 
    </form> 
</body> 
</html> 

Login.aspx.cs

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

namespace MultiligualApplication 
{ 
    public partial class LoginForm : System.Web.UI.Page 
    { 
     protected void Page_Load(object sender, EventArgs e) 
     { 
      if (!Page.IsPostBack) 
      { 
       Session["lang"] = "en-US"; 
      } 
     } 

     protected void ddlLanguage_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      Session["lang"] = ddlLanguage.SelectedItem.Value.ToString(); 
     } 
    } 
} 

英语课堂的一个文件

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

namespace MultiligualApplication 
{ 
    public class ClassEnLang 
    { 
     public static Dictionary<string, string> dictionary = new Dictionary<string, string>(); 
     static ClassEnLang() 
     { 
      dictionary.Add("loginhere", "Login Here"); 
      dictionary.Add("userid", "User Id"); 
      dictionary.Add("password", "Password"); 
      dictionary.Add("submit", "Submit"); 
     } 
    } 
} 

现在我得到的问题时<%=字典[”提交“]%>和文本框的占位符内的”字典“值设置。它没有显示出适当的价值。是我错过了一些东西,或者我正在使用一些错误的内联标签来获取值。请指导。

+3

你为什么不使用内置的本地化机制?基于resx文件。示例可以在这里找到 - http://msdn.microsoft.com/en-us/library/c6zyy3s9(v=vs.98).aspx –

+0

当然,我会尝试,但你能帮我设置值内的占位符使用当前代码? –

+0

@SergeyLitvinov我将如何使用全球化来设置占位符值,因为我将仅使用此权限为文本字段设置值?如果我错了,请纠正我。 –

回答

1

我记得你不能在服务器端控件中使用<%>标签。

所以,你可以采取由Bhavesh Kachhadiya建议的方法并删除runat属性,或者你可以在服务器端更新它们。 HTML代码示例:

<input id="txtUserId" type="button" value="button" runat="server" /> 

服务器端代码示例:

protected void Page_Load(object sender, EventArgs e) 
{ 
    // init this dict variable before 
    txtUserId.Attributes["placeholder"] = dict["userid"]; 
    if (!Page.IsPostBack) 
    { 
     Session["lang"] = "en-US"; 
    } 
} 
1

您是否获得以下原因的问题:

用你的标记没有runat="server"属性

使用此代码

<input id="txtPassword" type="text" placeholder="<%= dict['password']%>"/> 

而不是

<input id="txtPassword" type="text" runat="server" placeholder="<%= dict['password']%>"/> 
+0

感谢@Bhavesh Kachhadiya,但我正在使用此文本框值进一步使用。这两种事情都可以保持吗?即使用文本框值和安装程序? –

+0

是的,你可以使用文本框的值。 –

+0

如何?通过张贴表单? –