Last post Feb 09, 2007 09:26 PM by aweil
Feb 07, 2007 02:45 PM|Tom Ankers|LINK
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?!!!!
controlValue = Request.Form(languageCmdId.ToString())
controlValue = Session.Item(
controlValue = defaultLang.ToString()
buffer = Request.Form(eventTarget.ToString())
Feb 08, 2007 12:02 AM|aweil|LINK
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
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;
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...
Feb 08, 2007 05:56 AM|Tom Ankers|LINK
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.
Feb 08, 2007 11:09 AM|aweil|LINK
Feb 09, 2007 08:56 AM|Rubes|LINK
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:
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.
Feb 09, 2007 10:56 AM|Tom Ankers|LINK
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.
Feb 09, 2007 12:57 PM|Rubes|LINK
Thanks Tom - I gave it a try and it is working like a charm.
Feb 09, 2007 09:26 PM|aweil|LINK
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...