我正在尝试一些多语言网站,我在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>
</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");
}
}
}
现在我在&lt;%= dict [“submit”]%&gt;时遇到问题并设置文本框占位符内的“dict”值。它没有显示正确的值。我是否遗漏了某些东西,或者我使用了一些错误的内联标记来获取值。请指导。
答案 0 :(得分:1)
您因以下原因而遇到问题:
使用不带runat="server"
属性
使用此代码
<input id="txtPassword" type="text" placeholder="<%= dict['password']%>"/>
而不是
<input id="txtPassword" type="text" runat="server" placeholder="<%= dict['password']%>"/>
答案 1 :(得分:1)
我记得你不能使用&lt; %%&gt;服务器端控件中的标记。
因此,您可以使用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";
}
}