using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : Page
{
    private string _filterExpression;
    protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
    {
        if (e.InputParameters.Contains("sortExpressions"))
        {
            if (e.ExecutingSelectCount)
                e.InputParameters.Remove("sortExpressions");
        }
        else
        {
            e.InputParameters.Add("sortExpressions", RadGrid1.MasterTableView.SortExpressions.GetSortString());
        }
        _filterExpression = RadGrid1.MasterTableView.FilterExpression;
        e.InputParameters["filterExpressions"] = _filterExpression;

        //this is needed in order to workaround ObjectDataSource limitation of sorting IEnumerable
        e.Arguments.SortExpression = string.Empty;
    }

    protected void ObjectDataSource1_Selected(object sender, ObjectDataSourceStatusEventArgs e)
    {
        if (!(e.ReturnValue is IEnumerable))
        {
            //needed to bypass RadGrid's internal filtering
            RadGrid1.MasterTableView.FilterExpression = string.Empty;
        }
    }

    protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
        if (!string.IsNullOrEmpty(_filterExpression))
        {
            RadGrid1.MasterTableView.FilterExpression = _filterExpression;
        }
    }
}