0

I have a drop-down asp:listitem box that looks perfectly clean to me. It functions like when the first time you change a language on that page (Say, English to Swedish), it does change the text to that language.

The second time you try to change the language (Swedish back to English -- or any lang), it just reloads the page and then you have to select English a second time to actually change the page back to English.

I tried removing lines from the code behind, one at a time, but nothing changed it. If it's possible to tell what could be the cause, any help would be truly appreciated!

My .aspx page is like this:

  <asp:DropDownList ID="ddlCulture" DataTextField="DisplayName"
  DataValueField="Name" runat="server" BackColor="#DDE4EC" ForeColor="#465C71"
  Width="130px"  AutoPostBack="True">
  <asp:ListItem Selected="True" meta:resourcekey="MenuItemResourcelangen9">Select
  Language</asp:ListItem>
  <asp:ListItem Value="en" meta:resourcekey="MenuItemResourcelangen1"></asp:ListItem>
  <asp:ListItem Value="es-MX" meta:resourcekey="MenuItemResourcelangen2">
  </asp:ListItem>
  <asp:ListItem Value="fr" meta:resourcekey="MenuItemResourcelangen3"></asp:ListItem>
  <asp:ListItem Value="de" meta:resourcekey="MenuItemResourcelangen4"></asp:ListItem>
  <asp:ListItem Value="sv" meta:resourcekey="MenuItemResourcelangen5"></asp:ListItem>
  <asp:ListItem Value="ja" meta:resourcekey="MenuItemResourcelangen6"></asp:ListItem>
  <asp:ListItem Value="nl" meta:resourcekey="MenuItemResourcelangen7"></asp:ListItem>
  <asp:ListItem Value="ru" meta:resourcekey="MenuItemResourcelangen8"></asp:ListItem>
  </asp:DropDownList>

My code behind is like this:

Partial Public Class Site
Inherits System.Web.UI.MasterPage

Private Property UICulture As String

Protected Sub InitializeCulture()
    If Session("culture") IsNot Nothing Then
        UICulture = Session("culture").ToString()
    End If
End Sub

Protected Sub ddlCulture_SelectedIndexChanged(sender As Object, e As System.EventArgs)
Handles ddlCulture.SelectedIndexChanged
    Session("uiculture") = ddlCulture.SelectedValue
    Session("culture") = ddlCulture.SelectedValue
    Server.Transfer(Request.Path)
End Sub
End Class
2
  • 1
    Try Response.Redirect(Request.Path) instead of Server.Trasfer Feb 6, 2012 at 3:43
  • That did it AVD! Great suggestion. I appreciate the help and the knowledge -- never thought of this until you mentioned it! Feb 6, 2012 at 9:33

2 Answers 2

1

InitializeCulture is called before the Control's event (SelectedIndexChanged).

If you use Response.Redirect(Request.RawUrl), the changed session settings are applied in InitializeCulture, and the page can be refreshed (F5) without a "post again?" message in the browser.

1
  • Made the change. Works great now. Thanks for taking the time, and teachin' me a little vb Devio! Feb 6, 2012 at 9:34
1

Instead of handling selected index changed event, I will suggest you to use Request object to get the selected culture in InitializeCulture overload. For example,

Protected Sub InitializeCulture()
   Dim selectedLanguage As String = Request.Form(ddlCulture.UniqueID)
   IF selectedLanguage IsNot Nothing Then
      Me.Culture = Me.UICulture = selectedLanguage
      Thread.CurrentThread.CurrentCulture = _
          CultureInfo.CreateSpecificCulture(selectedLanguage)
      Thread.CurrentThread.CurrentUICulture = New _
         CultureInfo(selectedLanguage)
   End If
   MyBase.InitializeCulture()
End Sub
1
  • Thank you for the time and response, Vinay. Both your answer and Devios made sense, and I finally got this headache to work. Thank you for the knowledge and the time! Feb 6, 2012 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.