Reputation: 1832
The ListView is displaying properly and the DataPager is displaying the buttons at the bottom, but when I click the NEXT button, the DataPager is not going to the next page of data.
ASPX page:
<div id="EventListing">
<asp:ListView ID="ListEvents" runat="server">
<LayoutTemplate>
<div ID="itemPlaceholder" runat="server">
</div>
<div id="pagerFormat">
<asp:DataPager ID="ListingDataPager" runat="server" PageSize="5" PagedControlID="ListEvents" QueryStringField="page" >
<Fields>
<asp:NextPreviousPagerField
FirstPageText="First"
LastPageText="Last"
NextPageText="Next"
PreviousPageText="Back" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
<ItemTemplate>
<table id="tblEvents">
<tr>
<td rowspan="4" id="Col1Events"></td>
<td rowspan="4" id="Col2Events"></td>
<td rowspan="4" id="Col3Events"></td>
<td class="tdEvents"><span style="font-weight:bold; font-size: 1em;"><%#Eval("EVENT_DATERANGE") %>: <%#Eval("EVENT_NAME") %></span></td>
</tr>
<tr>
<td class="tdEvents"> <span style="font-size: .9em;"><%#Eval("EVENT_DESC") %></span></td>
</tr>
<tr>
<td class="tdEvents"><span style="font-size: .9em;"><%#Eval("EVENT_STREET") %>, <%#Eval("CITY.CITY_NAME") %></span></td>
</tr>
<tr>
<td class="tdEvents"><span style="font-size: .9em;"><%#Eval("EVENT_PHONE") %></span></td>
</tr>
<tr>
<td colspan="4" id="tdEmpty"></td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
</div>
Code Behind Page:
protected void btnFindEvents_Click(object sender, EventArgs e)
{
DateTime StartDt;
string EventType = ddlEventType.SelectedValue;
string dt = Request.Form["DatePickername"];
if (ddlEventType.SelectedIndex == 0)
{
EventType = "";
}
if (dt != "")
{
StartDt = Convert.ToDateTime(Request.Form["DatePickerName"]);
}
else
{
StartDt = DateTime.Now;
}
string CityName= ddlEventCity.SelectedValue;
if (ddlEventCity.SelectedIndex == 0)
{
CityName = "";
}
if ((ddlEventType.SelectedIndex == 0) && (ddlEventCity.SelectedIndex == 0))
{
//(1) ALL EVENTS
BLgetEvents obj = new BLgetEvents();
var EventList = obj.getAllEvents(StartDt);
ListEvents.DataSource = EventList;
ListEvents.DataBind();
}
}
Upvotes: 0
Views: 7440
Reputation: 38598
you have to bind data again on the ListView when you page by DataPager. So, you need to implement this bind on PagePropertiesChanging
event of your ListView control. Something like:
// 1 - add a method to bind ListView, add some parameters if you need
protected void BindListView()
{
var data = // get data from database
ListEvents.DataSource = data;
ListEvents.DataBind();
}
// 2 - call this method on the button
protected void btnFindEvents_Click(object sender, EventArgs e)
{
BindListView();
}
// 3 - call this method on the PagePropertiesChanging event of the ListView
protected void ListEvents_PagePropertiesChanging(object sender, PagePropertiesChangingEventArgs e)
{
//set current page startindex, max rows and rebind to false
YourDataPagerControlId.SetPageProperties(e.StartRowIndex, e.MaximumRows, false);
//rebind List View
BindListView();
}
And on your asp.net webform, you need to set the event on the listview tag:
<asp:ListView ID="ListEvents" runat="server" OnPagePropertiesChanging="ListEvents_PagePropertiesChanging">
...
</asp:ListView>
When you set the datasource from code behine, the same principle is valid for other databound controls like GridView, DataList, Repeater etc (you need to bind again on some paging event). An alternative way to solve this, you could add a datasource control (like objectdatasource, linqdatasource, etc...) and set it on the ListView's DataSourceID
property and the DataPager will work automatically.
For more details, look at this link: http://weblogs.asp.net/hajan/archive/2011/09/08/paging-listview-using-datapager-without-using-datasource-control.aspx
Upvotes: 1