I've got a "Select Language" drop-down on the top-right of the home page.
When I select a new language, the drop-down fires a postback and sets a
cookie and session value for "LanguageSelect ion". E.g. = "fr-FR"
However after this I still need to press F5 to refresh the webpage before
code in the page uses the new session variable value to display the
different language. Any ideas how to fix this?
Partial Class pages_master_pa ge_MasterPage
Inherits System.Web.UI.M asterPage
Function fSetLanguage() As Boolean
'Check if session language exists
'If not, get cookie, if none, get browser language
'and set cookie and session language
If IsPostBack = False Then
If Session("strLan guageSetting") = Nothing Then
'Get cookie
If Response.Cookie s("SiteLanguage ").Value = "" Then
Response.Cookie s("SiteLanguage ").Value =
fGetBrowserLang uage()
End If
Session("strLan guageSetting") =
Response.Cookie s("SiteLanguage ").Value
End If
DropDownList1.S electedValue = Session("strLan guageSetting")
End If
End Function
Function fLanguageChange () As Boolean
Session("strLan guageSetting") = DropDownList1.S electedValue
Response.Cookie s("SiteLanguage ").Value =
Session("strLan guageSetting")
Response.Cookie s("SiteLanguage ").Expires =
DateTime.Now.Ad dDays(1000)
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
fSetLanguage()
fSetLanguageVal ues()
End Sub
Protected Sub DropDownList1_S electedIndexCha nged(ByVal sender As Object,
ByVal e As System.EventArg s) Handles DropDownList1.S electedIndexCha nged
fLanguageChange ()
End Sub
Function fGetBrowserLang uage() As String
Dim objUserInfo() As String
Dim strBrowserLangu age As String
Dim strWhere As String
objUserInfo = Request.UserLan guages
strBrowserLangu age = objUserInfo(0)
'If an active language then set
strWhere = "LanguageCode=' " + strBrowserLangu age + "'"
If SharedFunctions .fGetSingleValu eFromTable("Act ive",
"tblLanguageCod es", strWhere) = True Then
fGetBrowserLang uage = strBrowserLangu age
Exit Function
End If
'If not, regress to root active language, e.g. fr-CA would be fr-FR
strWhere = "LanguageCodeGe neric='" + Left(strBrowser Language,
InStr(strBrowse rLanguage, "-") - 1) + "' AND [Active]= 'True'"
Dim strFoundLanguag eCode As String
strFoundLanguag eCode =
SharedFunctions .fGetSingleValu eFromTable("Lan guageCode", "tblLanguageCod es",
strWhere)
If strFoundLanguag eCode <"" Then
fGetBrowserLang uage = strFoundLanguag eCode
Exit Function
End If
'If not active, regress to en-US
fGetBrowserLang uage = "en-US"
End Function
Function fSetLanguageVal ues() As Boolean
Label1.Text = sfLanguage.fTex t(30)
Label2.Text = sfLanguage.fTex t(31)
Label3.Text = sfLanguage.fTex t(32)
HyperLink1.Text = sfLanguage.fTex t(33)
End Function
End Class
When I select a new language, the drop-down fires a postback and sets a
cookie and session value for "LanguageSelect ion". E.g. = "fr-FR"
However after this I still need to press F5 to refresh the webpage before
code in the page uses the new session variable value to display the
different language. Any ideas how to fix this?
Partial Class pages_master_pa ge_MasterPage
Inherits System.Web.UI.M asterPage
Function fSetLanguage() As Boolean
'Check if session language exists
'If not, get cookie, if none, get browser language
'and set cookie and session language
If IsPostBack = False Then
If Session("strLan guageSetting") = Nothing Then
'Get cookie
If Response.Cookie s("SiteLanguage ").Value = "" Then
Response.Cookie s("SiteLanguage ").Value =
fGetBrowserLang uage()
End If
Session("strLan guageSetting") =
Response.Cookie s("SiteLanguage ").Value
End If
DropDownList1.S electedValue = Session("strLan guageSetting")
End If
End Function
Function fLanguageChange () As Boolean
Session("strLan guageSetting") = DropDownList1.S electedValue
Response.Cookie s("SiteLanguage ").Value =
Session("strLan guageSetting")
Response.Cookie s("SiteLanguage ").Expires =
DateTime.Now.Ad dDays(1000)
End Function
Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArg s) Handles Me.Load
fSetLanguage()
fSetLanguageVal ues()
End Sub
Protected Sub DropDownList1_S electedIndexCha nged(ByVal sender As Object,
ByVal e As System.EventArg s) Handles DropDownList1.S electedIndexCha nged
fLanguageChange ()
End Sub
Function fGetBrowserLang uage() As String
Dim objUserInfo() As String
Dim strBrowserLangu age As String
Dim strWhere As String
objUserInfo = Request.UserLan guages
strBrowserLangu age = objUserInfo(0)
'If an active language then set
strWhere = "LanguageCode=' " + strBrowserLangu age + "'"
If SharedFunctions .fGetSingleValu eFromTable("Act ive",
"tblLanguageCod es", strWhere) = True Then
fGetBrowserLang uage = strBrowserLangu age
Exit Function
End If
'If not, regress to root active language, e.g. fr-CA would be fr-FR
strWhere = "LanguageCodeGe neric='" + Left(strBrowser Language,
InStr(strBrowse rLanguage, "-") - 1) + "' AND [Active]= 'True'"
Dim strFoundLanguag eCode As String
strFoundLanguag eCode =
SharedFunctions .fGetSingleValu eFromTable("Lan guageCode", "tblLanguageCod es",
strWhere)
If strFoundLanguag eCode <"" Then
fGetBrowserLang uage = strFoundLanguag eCode
Exit Function
End If
'If not active, regress to en-US
fGetBrowserLang uage = "en-US"
End Function
Function fSetLanguageVal ues() As Boolean
Label1.Text = sfLanguage.fTex t(30)
Label2.Text = sfLanguage.fTex t(31)
Label3.Text = sfLanguage.fTex t(32)
HyperLink1.Text = sfLanguage.fTex t(33)
End Function
End Class
Comment