Localizing Filter Controls


I am fully localizing a new ASP.NET 4 Dynamic Data web site, and there’s more than meets the eye to doing so. I found that the Boolean filter User Control would not update correctly, when switching languages; it would keep the items (All, True, and False) in the previously selected culture. So, I had a look at the code, after adding the localization calls (no, I don’t localize the exceptions ;-)):

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    If Not Column.ColumnType.Equals(GetType(System.Boolean)) Then
        Throw New InvalidOperationException(String.Format("A boolean filter was loaded for column '{0}' but the column has an incompatible type '{1}'.", Column.Name, Column.ColumnType))
    End If

    If Not Page.IsPostBack Then
        DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "AllItems"), String.Empty))

        If Not Column.IsRequired Then
            DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "NotSet"), NullValueString))
        End If

        DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "True"), Boolean.TrueString))
        DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "False"), Boolean.FalseString))
        ' Set the initial value if there is one
        Dim initialValue As String = DefaultValue

        If Not String.IsNullOrEmpty(initialValue) Then
            DropDownList1.SelectedValue = initialValue
        End If
    End If
End Sub

Well, that didn’t take too long to work out; the items are only added to the DropDownList control on the initial page view. Okay, so I modified the code:

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    If Not Column.ColumnType.Equals(GetType(System.Boolean)) Then
        Throw New InvalidOperationException(String.Format("A boolean filter was loaded for column '{0}' but the column has an incompatible type '{1}'.", Column.Name, Column.ColumnType))
    End If

    DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "AllItems"), String.Empty))

    If Not Column.IsRequired Then
        DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "NotSet"), NullValueString))
    End If

    DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "True"), Boolean.TrueString))
    DropDownList1.Items.Add(New ListItem(GetGlobalResourceObject("Strings", "False"), Boolean.FalseString))

    If Not Page.IsPostBack Then
        ' Set the initial value if there is one
        Dim initialValue As String = DefaultValue

        If Not String.IsNullOrEmpty(initialValue) Then
            DropDownList1.SelectedValue = initialValue
        End If
    End If
End Sub

Now that should work, but it doesn’t… Final thing to fix, is to disable view state for the user Control, so I added EnableViewState=”false” to the Control directive in the markup, like this:

<%@ Control Language="VB" CodeFile="Boolean.ascx.vb" Inherits="BooleanFilter" EnableViewState="false" %> 

That works 🙂

Advertisements