2016-08-05 138 views
0

事實上,我對ASP.NET仍然很陌生,一直在嘗試構建一個簡單的Web應用程序,它包含gridview以從數據庫中檢索數據並將其顯示給用戶和用戶可以通過按下單選按鈕來改變語言,以便從英文更改爲阿拉伯語,反之亦然,但不幸的是,當我第一次加載頁面時選擇阿拉伯語時,什麼都沒有發生,並且它保持英文頁面,除非直到我按英文單選按鈕。無法在運行時使用會話更改語言ASP.NET

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

回答

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_LoadLangRadioButton_SelectedIndexChanged這就是全部;-)

+0

普拉茲非常感謝你工作得像一個魅力 我剛剛刪除了這部分 if(String.IsNullOrEmpty(Session [「Lang」])) Session [「Lang」] =「en-US」; } –