locked
Master pages and dynamic localization RRS feed

  • Question

  • User83354550 posted

    Hi,

    I have a website under development which requires dynamic billingiual requirements based on a dropdown box at the top of the master page. I have the almost complete this and below is the current status of development:

    Master page created for which all aspx pages inherit, this master page contains two content areas, one of which contains the drop down box which is a custom control (please note: the drop down is placed in the master page and not the same content area available in the aspx pages). In order to be able to change images, text etc in the master page when a language setting is selected, I have written the below BasePage which overrides the initialize_culture event within the page life cycle.

    The site fundamentaly works, except for the following problem. Within my BasePage I check if the current request is a postback(by attempting to retrieve an "_EVENTTARGET" value, if this value is not null then I compare it to constant ID of the control contained within my master page and then set the cuurent thread UICulture using a seperate code file (which works fine). The problem is I cant get a value from the _EVENTTARGET field. can anyone help?!!!!

    'constant id of control containing lanuage setting

    Const languageCmdId As String = "ctl00$Contentplaceholder2$cmdLanguage$cmdLanguage"

    Const defaultLang As String = "en-gb"

    Const eventTarget As String = "__EVENTTARGET"

    Protected Overrides Sub InitializeCulture()

    Dim controlValue As String = ""

    Dim controlId As String = ""

    If (isPost() = True) Then

    If (Request.Form(eventTarget.ToString()) _

    .ToString().Equals(languageCmdId.ToString())) Then

    controlValue = Request.Form(languageCmdId.ToString())

    End If

    Else

    Try

    controlValue = Session.Item("userLanguage").ToString()

    Catch ex As Exception

    controlValue = defaultLang.ToString()

    End Try

    End If

    End Sub

    'private function as isPostBack is not available

    Private Function isPost() As Boolean

    Dim buffer As String = ""

    Try

    buffer = Request.Form(eventTarget.ToString())

    Catch ex As Exception

    buffer = ""

    End Try

    If (buffer.ToString().Equals("")) Then

    Return False

    Else

    Return True

    End If

    End Function

    Wednesday, February 7, 2007 2:45 PM

Answers

  • User764555827 posted

    Hi Tom,

    You're indeed almost there. A few more steps and you'll be done! 

    What you're trying to do is to parse the HTTP POST that you're getting, because the InitializeCulture method is called before you even get the DropDownList_Changed event.

    I believe reading HTTP POST in ASP.net is a quite bad solution. A better one I'd recommend is to simply ask for your page to reload inside the DropDownList_Changed event (that is, when you get an event telling you that the user chose another language).

    Just look at the code I'd recommend you place in the CodeBehind of your master page:

        protected void UseEnglishAction_Click(object sender, ImageClickEventArgs e)
        {
            HttpContext context = HttpContext.Current;
            context.Session["Language"] = new CultureInfo("en-GB");
            // Re-process current page from the start
            context.Server.Transfer(context.Request.Path);
        }

    And in your BasePage: 

        protected override void InitializeCulture()
        {
            CultureInfo preferredCulture = HttpContext.Current.Session["Language"] as CultureInfo;
            if (preferredCulture != null)
            {
                Thread.CurrentThread.CurrentUICulture = preferredCulture;
                Thread.CurrentThread.CurrentCulture = preferredCulture;
            }

            base.InitializeCulture();
        }

    That's it! The trick is to call Server.Transfer(context.Request.Path) in order to reload your page.

    It works like a charm on my site... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 8, 2007 12:02 AM

All replies

  • User764555827 posted

    Hi Tom,

    You're indeed almost there. A few more steps and you'll be done! 

    What you're trying to do is to parse the HTTP POST that you're getting, because the InitializeCulture method is called before you even get the DropDownList_Changed event.

    I believe reading HTTP POST in ASP.net is a quite bad solution. A better one I'd recommend is to simply ask for your page to reload inside the DropDownList_Changed event (that is, when you get an event telling you that the user chose another language).

    Just look at the code I'd recommend you place in the CodeBehind of your master page:

        protected void UseEnglishAction_Click(object sender, ImageClickEventArgs e)
        {
            HttpContext context = HttpContext.Current;
            context.Session["Language"] = new CultureInfo("en-GB");
            // Re-process current page from the start
            context.Server.Transfer(context.Request.Path);
        }

    And in your BasePage: 

        protected override void InitializeCulture()
        {
            CultureInfo preferredCulture = HttpContext.Current.Session["Language"] as CultureInfo;
            if (preferredCulture != null)
            {
                Thread.CurrentThread.CurrentUICulture = preferredCulture;
                Thread.CurrentThread.CurrentCulture = preferredCulture;
            }

            base.InitializeCulture();
        }

    That's it! The trick is to call Server.Transfer(context.Request.Path) in order to reload your page.

    It works like a charm on my site... 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, February 8, 2007 12:02 AM
  • User83354550 posted

    Fantastic, thats just what it needed. I now have it working perfect and very smooth. I hope you don't mind, I borrowed the idea of using flags for my English and Welsh language choices!

    Thank you very much for your help.

    Thursday, February 8, 2007 5:56 AM
  • User764555827 posted
    Sure I don't mind! I'm very glad I could help you, and congratulations on having your site localized.
    Thursday, February 8, 2007 11:09 AM
  • User665349069 posted

    Hey Guys, I hope you don't mind me posting a follow up question on this, but this is essentially the exact same code I am using on my site with one exception.  In my click event I'm using a Redirect:

     Response.Redirect(Request.UrlReferrer.AbsoluteUri)

    Any chance one of you could explain the benefit of using the server.transfer instead?  I assume it is just less intensive, but want to make sure before I ammend my code.

     Thanks

    Friday, February 9, 2007 8:56 AM
  • User83354550 posted

    Hi Rubes,

    You more or less hit the nail on the head. Reponse.Redirect() takes a round trip to the server when used and comes back with everything the server gives it, this can result in a lot of heavier traffic. Server.Transfer() on the other hand, does not take a round trip to the server, it does exactly what it says - transfers the current request to the specified file or domain, this is good when dealing with a situation where there is a lot of web traffic and should be a lot faster then the previous method.

    Cheers

    Tom

    Friday, February 9, 2007 10:56 AM
  • User665349069 posted

    Thanks Tom - I gave it a try and it is working like a charm. 

    Friday, February 9, 2007 12:57 PM
  • User764555827 posted

    I Rubes. Tom already answered (thanks Tom).

    One more interest of the Server.Transfer method is that the user doesn't know his request has been redirected. In our case, this is not a benefit since we're remaining on the same page, but in other cases it's of great interest.

    For instance, when I detect that a user tampers with my applications, I Server.Transfer him to a page which tells him that his playing around was detected. That way, the user believes that the same page he's trying to tamper with his sending him that message, and will never know about the real page that does the displaying job...

    Best regards,

    Arnaud 

    Friday, February 9, 2007 9:26 PM