locked
Globalization/Localization: LinkButton Control RRS feed

  • Question

  • User1862342363 posted

    I need some help deciding a correct implementation of what I am trying to do.

    I have a web application that uses Globalization/Localization.  My web app will only supports particular locales.  For example, the web app only supports en-US, es-US, fr-FR (English and Spanish languages in the United States and French in France).  I have a process in place that creates a drop down list of countries supported, so my drop down list values are United States and France.  Then in the drop down dropdownlist_SelectedIndexChanged event I pass the country selected to a method that returns the supported languages in the countries.  It passes back a datatable.  I then bind the datatable to a repeater control.  In the repeater control I create LinkButtons.  One LinkButton for each language supported by the Country selected in the drop down list, so if I select United States 2 LinkButtons are created (one for Spanish and one for English).  All of this is working as expected.

    Need help with the following:

    The way my pages render in the correct languages is by posting back (i.e. Response.Redirect(Request.RawUrl)), but before this line of code I set a session variable to the correct locale (i.e. Session["culture"] = "fr-FR") this is how the correct resource file is used.

    My question is how to post back using the dynamically created LinkButtons?  I set the text of the LinkButton controls using the datatable (i.e. <asp:LinkButton ID="linkButton" runat="server" Text='<%# Eval("language") %>'></asp:LinkButton>

    What I need to happen is when I click on one of the languages (LinkButtons) I need to post back to the same page, but set the Session["Culture"] using the correct locale.  The datatable also has the locale.  LinkButton doesnt have a property I can use to save the locale to.  The other problem is that the LinkButton controls are dynamically created.

    Any help would be greatly appreciated. Thanks...

    Sunday, August 9, 2009 6:42 PM

Answers

  • User-126244515 posted

    Well, I did misread your post somewhat, but when you dynamically create your LinkButton controls, you can add a client-side event. First though, add a HiddenField to your markup that is accesible both client-side and server-side, like this:

    <input name="Hidden1" id="Hidden1" type="hidden" runat="server" />


    Then, where you respond to the SelectedIndexChanged event, try this code:

    Protected Sub CountriesDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CountriesDropDownList.SelectedIndexChanged
        Select Case CountriesDropDownList.SelectedValue
            Case "USA"
                ...
            Case "France"
                Dim frFRLinkButton As New LinkButton()
                frFRLinkButton.Text = "fr-FR"
                frFRLinkButton.Attributes.Add("onclick", "javascript:document.getElementById('Hidden1').value = 'fr-FR';")
                LinkButtonsContainer.Controls.Add(frFRLinkButton)
        End Select
    End Sub

    Then you can examine the value of the HiddenField in say your Page.Load event, like this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.IsPostBack Then
            Response.Write(Hidden1.Value)
        End If
    End Sub


    I know I'm just writing out the value of the control, but I'm sure you get the idea. If you need to the corresponding C# code, let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 11, 2009 4:08 AM

All replies

  • User-126244515 posted

    Have you considered using an HTML HiddenField element and set the value of this element when a user selects a language, i.e. before the LinkButton is clicked?

    Monday, August 10, 2009 1:47 AM
  • User1862342363 posted

    How exactly would that work?

    Thanks,

    Monday, August 10, 2009 10:13 AM
  • User-126244515 posted

    Well, I did misread your post somewhat, but when you dynamically create your LinkButton controls, you can add a client-side event. First though, add a HiddenField to your markup that is accesible both client-side and server-side, like this:

    <input name="Hidden1" id="Hidden1" type="hidden" runat="server" />


    Then, where you respond to the SelectedIndexChanged event, try this code:

    Protected Sub CountriesDropDownList_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles CountriesDropDownList.SelectedIndexChanged
        Select Case CountriesDropDownList.SelectedValue
            Case "USA"
                ...
            Case "France"
                Dim frFRLinkButton As New LinkButton()
                frFRLinkButton.Text = "fr-FR"
                frFRLinkButton.Attributes.Add("onclick", "javascript:document.getElementById('Hidden1').value = 'fr-FR';")
                LinkButtonsContainer.Controls.Add(frFRLinkButton)
        End Select
    End Sub

    Then you can examine the value of the HiddenField in say your Page.Load event, like this:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Me.IsPostBack Then
            Response.Write(Hidden1.Value)
        End If
    End Sub


    I know I'm just writing out the value of the control, but I'm sure you get the idea. If you need to the corresponding C# code, let me know.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 11, 2009 4:08 AM