ListView DataPager不是分页

时间:2012-07-11 00:34:46

标签: asp.net listview linq-to-entities datapager

ListView正常显示,DataPager正在底部显示按钮,但是当我单击NEXT按钮时,DataPager不会转到下一页数据。

ASPX页面:

    <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") %>:&nbsp;&nbsp;<%#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>

页面背后的代码:

        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();
            }
}

1 个答案:

答案 0 :(得分:1)

当您按DataPager分页时,必须在ListView上再次绑定数据。因此,您需要在ListView控件的PagePropertiesChanging事件上实现此绑定。类似的东西:

// 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();
}

在你的asp.net webform上,你需要在listview标签上设置事件:

<asp:ListView ID="ListEvents" runat="server" OnPagePropertiesChanging="ListEvents_PagePropertiesChanging">
...
</asp:ListView>

当您从代码behine设置数据源时,相同的原则对于其他数据绑定控件(如GridView,DataList,Repeater等)也是有效的(您需要在某些分页事件上再次绑定)。另一种解决方法是,您可以添加一个数据源控件(如objectdatasource,linqdatasource等...)并将其设置在ListView的DataSourceID属性上,DataPager将自动运行。

有关详细信息,请查看以下链接:http://weblogs.asp.net/hajan/archive/2011/09/08/paging-listview-using-datapager-without-using-datasource-control.aspx