ListView paging with dynamic datasource

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.

Advertisements
This entry was posted in ASP.NET and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s