开发者

2 postbacks required to change language in multilanguage website using imagebutton

开发者 https://www.devze.com 2023-03-24 20:04 出处:网络
I have successfully implemented a multi-language website in which user can change language by clicking one of the imagebuttons.

I have successfully implemented a multi-language website in which user can change language by clicking one of the imagebuttons. I used a Hiddenfield to store code of language that was selected by the user (on Imagebutton click event). My InitializeCulture method looks like this:

     protected override void InitializeCulture()
    {
    string culture = "Auto";
    string selectedValue = Request.Form["ctl00$HiddenFieldLang"];
    switch (selectedValue)
    {
        case "1": culture = "Auto";
            break;
        case "2": culture = "zh-HK";
            break;
        default: break;
    }

    if (culture != "Auto")
    {
        System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo(culture);
        //ci = System.Globalization.CultureInfo.CreateSpecificCult开发者_StackOverflowure(culture);
        System.Threading.Thread.CurrentThread.CurrentCulture = ci;
        System.Threading.Thread.CurrentThread.CurrentUICulture = ci;
    }
    base.InitializeCulture();
    }

This works fine. But it takes 2 postbacks to change the language of the page. I'm guessing, in one postback (which changes nothing), the hiddenfield's value is set. In second postback, actual translation takes place.

How do I implement it so that the page is translated in just one click of imagebutton?

Thanks!!


You may add client-side handler for imagebutton's click event and set value on the HiddenFieldLang on it. Or you can get an control that caused a postback in InitializeCulture method and if it one of buttons used for switching a language then apply appropriate culture.

The first option in my opinion is preferred as you don't need check which control cause postback each time.


I have implemented the Multi-Language site in ASP.NET and had some issue once... you can find the required code in the Question with an problem's solution regarding Hyperlink in the answer by me itself. And here I think you have to find the Image control and not the HiddenField control


Thank you all for the replies. I managed to solve this problem with the help of this link.

But I have another issue now. I have a masterpage and I put the Hiddenfield and the Javascript function to set the value of hiddenfield in my masterpage. In the URL of all my pages, I have a querystring with the language id. So, whenever the language changes, I want to change the querystring as well.

Is there any way I can fetch the URL of current content page from the masterpage and do a Response.Redirect or Server.Transfer from SetHiddenValue(val) function itself and ensure that the text is translated?

0

精彩评论

暂无评论...
验证码 换一张
取 消