views:

105

answers:

1

I have a DataList and FormView; they have separate datasources, though they pull the same info. The FormView's datasource has a FilterExpression to pull whatever's been selected on the DataList. On first load, the SelectedValue of the DataList is null (naturally). I expect the FilterExpression to result in zero rows, but it doesn't. If I set the DefaultValue to 0, it does, but then the parameter never updates when I select something from the DataList. Am I doing it wrong?

A: 

Turns out, (according to this post) there's a bug with this. The solution is to attach an OnFiltering handler like this (I've made a few enhancements):

protected void FilteringCheck(Object sender, SqlDataSourceFilteringEventArgs e)
{
    // Make sure there are no null parameters.
    for (int i = 0; i < e.ParameterValues.Count; i++)
    {
        if (e.ParameterValues[i] == null)
        {
            switch (((System.Web.UI.WebControls.SqlDataSourceView)sender).FilterParameters[i].Type)
            {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                    e.ParameterValues[i] = -1;
                    break;
                case TypeCode.Byte:
                    e.ParameterValues[i] = 0;
                    break;
                case TypeCode.Char:
                case TypeCode.String:
                    e.ParameterValues[i] = string.Empty;
                    break;
                case TypeCode.DateTime:
                    e.ParameterValues[i] = new DateTime();
                    break;
            }
        }
    }
}
end-user