Binding a Listview with a design time datasource is very convenient, and you can utilize the default paging function of DataPager. To use a dynamically assigned datasource with minimum code for paging, do this:
Method 1:
In Page.aspx:
<asp:DataPager ID=”DataPager1″ runat=”server” PageSize=”20″ onprerender=”DataPager1_PreRender”>
In Page.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
ListView1.DataSource = DB.GetPremiumsByCompany(Security.SessionCompanyID);
ListView1.DataBind();
}
}protected void DataPager1_PreRender(object sender, EventArgs e)
{
if (IsPostBack)
{
ListView1.DataSource = DB.GetPremiumsByCompany(Security.SessionCompanyID);
ListView1.DataBind();
}
}
Method 2:
In Page.aspx
<asp:ListView OnPagePropertiesChanging=”lvProducts_OnPagePropertiesChanging” …>
In Page.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack == false)
{
BindMyData();
}
}protected void lvProducts_OnPagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
myPager.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
BindMyData();
}
====
Method 1 won’t work with querystring mode of the pager.