无法在运行时ASP.NET使用会话更改语言

时间:2016-08-05 11:23:59

标签: c# asp.net

事实上,我还是ASP.NET新手,并且一直在尝试构建一个包含gridview的简单Web应用程序,以便从数据库中检索数据并将其显示给用户,用户可以通过按收音机来更改语言按钮从英语更改为阿拉伯语,反之亦然但不幸的是,当我第一次加载页面时,我选择阿拉伯语没有任何反应,除非我按下英语单选按钮,否则页面保持英文。

P.S。请检查附件以完全理解我的问题。

这是我的HTML代码

<div>
    &nbsp;<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);
    }
}

demo for the encountered problem

1 个答案:

答案 0 :(得分:0)

This MSDN article描述了如何以编程方式更改网页文化。事实上, 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 中删除无用的代码,这就是全部; - )