可以';t在运行时使用会话ASP.NET更改语言
本文关键字:ASP 会话 NET 语言 运行时 可以 | 更新日期: 2023-09-27 18:01:03
事实上,我对ASP.NET还是个新手,一直在尝试构建一个包含gridview的简单web应用程序,从数据库中检索数据并将其显示给用户,用户可以通过按下单选按钮来更改语言,从英语更改为阿拉伯语,反之亦然,但不幸的是,当我第一次加载页面时选择阿拉伯语时,什么都没有发生,它保持除非并且直到我按下英语单选按钮。
附言:请查看附件,以充分了解我的问题。
这是我的HTML代码
<div>
<asp:RadioButtonList ID="LangRadioButton" AutoPostBack="true" runat="server" RepeatColumns="2" meta:resourcekey="LangRadioButtonResource1"
OnSelectedIndexChanged="LangRadioButton_SelectedIndexChanged">
<asp:ListItem Text="English" Value="en-US" Selected="True"></asp:ListItem>
<asp:ListItem Text="Arabic" Value="ar-YE"></asp:ListItem>
</asp:RadioButtonList>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None"
BorderWidth="1px" CellPadding="3" CellSpacing="2"
DataSourceID="ObjectDataSource1" DataKeyNames="Id" meta:resourcekey="GridView1Resource1">
<Columns>
<asp:CommandField ShowSelectButton="True" meta:resourcekey="CommandFieldResource1" />
<asp:BoundField DataField="Id" HeaderText="Id" SortExpression="Id" meta:resourcekey="BoundFieldResource1" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" meta:resourcekey="BoundFieldResource2" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" meta:resourcekey="BoundFieldResource3" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3"
CellSpacing="2" DataSourceID="ObjectDataSource2" Height="50px" Width="125px" DataKeyNames="Id"
OnItemDeleted="DetailsView1_ItemDeleted" OnItemInserted="DetailsView1_ItemInserted"
OnItemUpdated="DetailsView1_ItemUpdated" meta:resourcekey="DetailsView1Resource1">
<EditRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White"/>
<Fields>
<asp:BoundField DataField="Id" InsertVisible="false" ReadOnly="true" HeaderText="Id" SortExpression="Id" meta:resourcekey="BoundFieldResource4" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" meta:resourcekey="BoundFieldResource5" />
<asp:BoundField DataField="City" HeaderText="City" SortExpression="City" meta:resourcekey="BoundFieldResource6" />
<asp:BoundField DataField="PhoneNo" HeaderText="PhoneNo" SortExpression="PhoneNo" meta:resourcekey="BoundFieldResource7" />
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" meta:resourcekey="CommandFieldResource2" />
</Fields>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
</asp:DetailsView>
<br />
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetAllEmployeesBasicDetails" TypeName="CascadingDDL.EmployeeDataAccessLayer"></asp:ObjectDataSource>
<asp:ObjectDataSource ID="ObjectDataSource2" runat="server" SelectMethod="GetAllEmployeesFullDetailsByUd"
TypeName="CascadingDDL.EmployeeDataAccessLayer" DeleteMethod="DeleteEmployee" InsertMethod="InsertEmployee"
UpdateMethod="UpdateEmployee">
<DeleteParameters>
<asp:Parameter Name="Id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PhoneNo" Type="Int32" />
</InsertParameters>
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" Name="Id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Id" Type="Int32" />
<asp:Parameter Name="Name" Type="String" />
<asp:Parameter Name="City" Type="String" />
<asp:Parameter Name="PhoneNo" Type="Int32" />
</UpdateParameters>
</asp:ObjectDataSource>
</div>
这是后面的代码
public partial class DetailsviewInUpDe : System.Web.UI.Page
{
protected override void InitializeCulture()
{
if (Session["Lang"] != null)
{
string cultureName = Session["Lang"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
}
base.InitializeCulture();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["Lang"] == null)
{
Session["Lang"] = "en-US";
}
}
}
protected void LangRadioButton_SelectedIndexChanged(object sender, EventArgs e)
{
Session["Lang"] = LangRadioButton.SelectedValue.ToString();
}
protected void Page_PreRender(object sender, EventArgs e)
{
if (CultureInfo.CurrentUICulture.Name == "ar-YE")
{
form1.Attributes["dir"] = ((string)GetLocalResourceObject("Dir")).ToString();
}
else
{
form1.Attributes["dir"] = ((string)GetLocalResourceObject("Dir")).ToString();
}
if (GridView1.SelectedRow == null)
{
DetailsView1.Visible = false;
}
else
{
DetailsView1.Visible = true;
}
}
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
GridView1.DataBind();
GridView1.SelectRow(-1);
}
protected void DetailsView1_ItemDeleted(object sender, DetailsViewDeletedEventArgs e)
{
GridView1.DataBind();
GridView1.SelectRow(-1);
}
protected void DetailsView1_ItemUpdated(object sender, DetailsViewUpdatedEventArgs e)
{
GridView1.DataBind();
GridView1.SelectRow(-1);
}
}
遇到的问题的演示
这篇MSDN文章描述了如何以编程方式更改页面的区域性。事实上,在页面处理过程中,InitializeCulture方法的调用时间远远早于LangRadioButton_SelectedIndexChanged事件处理程序。因此,不要使用事件处理程序来读取选定的值。
直接从InitializeCulture方法中的已发布数据(Request.Form名称/值集合(中读取所选值,方法如下:
protected override void InitializeCulture()
{
//read posted value
if (!String.IsNullOrEmpty(Request.Form["LangRadioButton"])) {
Session["Lang"] = Request.Form["LangRadioButton"];
}
//set default value if it's empty
if (String.IsNullOrEmpty(Session["Lang"]))
{
Session["Lang"] = "en-US";
}
string cultureName = Session["Lang"].ToString();
Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(cultureName);
Thread.CurrentThread.CurrentUICulture = new CultureInfo(cultureName);
base.InitializeCulture();
}
然后,从方法Page_Load和LangRadioButton_SelectedIndexChanged中删除无用的代码,仅此而已;-(