Data Browser - Viewing Site  Sector 23 Code Bank Logged in as:  Guest  




           


Reusable .NET Paged Data Source
Nothing is worse than having aspx files filled with complex data sources to implement paging.

Your database logic should be in the data layer.
Your paging logic should be in a reusable business layer.
Your page should just have a reusable control that asks the business layer to get the data.

Solution:
Data Layer - create a base Searcher class, and then implement Searchers for different types of data you are searching for.
Business Layer - create a base PagedDataSource, and then implement Sources for each type of data you are searching for.
UI Layer - drop the PagedDataSource on your page and in code behind, set the search criteria.

Highlights:

Searcher class needs the following methods at a minimum:
public DataTable GetPaged(string criteria1, bool? criteria2, int maximumRows, int startRowIndex, string sortExpression)
public int Count(string criteria1, bool? criteria2)
(use criteria as needed. Return a list, LINQ, DataTable etc as needed).
I'm not going to implement the whole thing for you... use LINQ, ADO, your data layer, etc, to get the data.
Add criteria as needed.

DataSource base looks like this:

/// <summary>
/// Base class for paged data sources. Sets new defaults for localization.
/// </summary>
public abstract class BasePagedDataSource : ObjectDataSource
{
public BasePagedDataSource()
: base()
{
base.EnablePaging = true;
base.SelectCountMethod = "Count";
base.SelectMethod = "GetPaged";
base.SortParameterName = "sortExpression"; // the default
}

/// <summary>
/// Whether control sorts paging
/// </summary>
[DefaultValue(true)]
public new bool EnablePaging
{
get { return base.EnablePaging; }
set { base.EnablePaging = value; }
}

/// <summary>
/// Method to retreive row count
/// </summary>
[DefaultValue("Count")]
public new string SelectCountMethod
{
get { return base.SelectCountMethod; }
set { base.SelectCountMethod = value; }
}

/// <summary>
/// Method to retreive row count
/// </summary>
[DefaultValue("GetViewPaged")]
protected new string SelectMethod
{
get { return base.SelectMethod; }
set { base.SelectMethod = value; }
}

/// <summary>
/// Sort Expression Parameter Name(loc
/// </summary>
[DefaultValue("sortExpression")]
public new string SortParameterName
{
get { return base.SortParameterName; }
protected set { base.SortParameterName = value; }
}

/// <summary>
/// Gets or sets the name of the class that the System.Web.UI.WebControls.ObjectDataSource object represents.
/// Users should not override this
/// </summary>
[DefaultValue("")]
protected new string TypeName
{
get { return base.TypeName; }
set { base.TypeName = value; }
}
} // class

Now for your custom paged source, you can extend it:

/// <summary>
/// A TestPagedDataSource specifically for paged Test searches.
/// </summary>
[ToolboxData("<{0}:TestPagedDataSource runat=server></{0}:TestPagedDataSource>")]
public class TestPagedDataSource : BasePagedDataSource
{
protected override void OnInit(EventArgs e)
{
base.TypeName = typeof(TestSearcher).FullName;
SelectParameters.Add("criteria1", TypeCode.String, "");
SelectParameters.Add("criteria2", TypeCode.Boolean, ""); // you should use constants here for the param names, this is just a demo!
}

public void SetParams(string criteria1, bool? criteria2)
{
SelectParameters["criteria1"].DefaultValue = criteria1; // you should use constants here for the param names, this is just a demo!
SelectParameters["criteria2"].DefaultValue = criteria2.ToString();
}
} // class


Your .aspx page:
<my:TestPagedDataSource runat="server" ID="odsTest" />
<asp:GridView id="gridTest"...>


Your .aspx.cs code behind:

this.odsTest.SetParams("hello", true);
this.gridTest.DataSourceID = odsTest.ID;

And you're done (mostly; there are a few more steps but you'll figure them out).

You can call setParams on each postback and change them; it will refresh your grid. You should probably also reset page index to 0 if you change filters.

Created By: amos 3/28/2014 12:25:51 PM
Updated: 3/28/2014 12:38:21 PM