ASP.NET中控件实现分页功能/Repeater控件分页

2018-06-22 07:47:26来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

//repeate.aspx  
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="repeate.aspx.cs" Inherits="repeate" %>  
<%@ Import Namespace="System.Data" %>  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml" >  
<head runat="server">  
    <title>无标题页</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" >  
        <HeaderTemplate><%-- 我是头模板--%>  
        <table width="500">  
        <tr style="background-color: #ccffcc;">  
        <td>作者</td>  
        <td>书籍</td>  
        </tr>  
        </HeaderTemplate>  
        <ItemTemplate><%--我是项模板--%>  
        <tr>  
        <td><a href='repeate.aspx?id=<%# Eval("au_id")%>'><%# Eval("au_lname") %></a></td>  
        <td><asp:Repeater ID="Repeater2" runat="server" DataSource='<%# Eval("myrela") %>'>  
        <ItemTemplate>  
        <%# Eval("[/"title_id/"]") %>  
        </ItemTemplate>  
        </asp:Repeater>  
        </td>  
        </tr>          
        </ItemTemplate>  
        <SeparatorTemplate><%--这是分隔线模板--%>  
        <tr>  
        <td colspan="2">  
        <hr style="border-top:1pt;"/>  
        </td>  
        </tr>  
        </SeparatorTemplate>  
        <FooterTemplate><%--这是脚模板--%>  
        <tr>  
        <td colspan="2" style="font-size:12pt;color:#0099ff; background-color:#e6feda;"><asp:Label ID="lblpc" runat="server" Text="Label"></asp:Label>页 当前为第  
        <asp:Label ID="lblp" runat="server" Text="Label"></asp:Label><asp:HyperLink ID="hlfir" runat="server" Text="首页"></asp:HyperLink>  
        <asp:HyperLink ID="hlp" runat="server" Text="上一页"></asp:HyperLink>  
        <asp:HyperLink ID="hln" runat="server" Text="下一页"></asp:HyperLink>  
        <asp:HyperLink ID="hlla" runat="server" Text="尾页"></asp:HyperLink>  
         跳至第  
         <asp:DropDownList ID="ddlp" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlp_SelectedIndexChanged" >  
         </asp:DropDownList></td>  
        </tr>  
        </table>  
        </FooterTemplate>  
        </asp:Repeater>  
         </div>  
    </form>  
</body>  
</html> 
//repeate.aspx.cs
using System;  
using System.Data;  
using System.Data.SqlClient;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
public partial class repeate : System.Web.UI.Page  
{  
    protected void Page_Load(object sender, EventArgs e)  
    {  
        if (!IsPostBack)  
        {  
            Repeater1.DataSource = pds();  
            Repeater1.DataBind();              
        }  
    }  
    private PagedDataSource pds()  
    {  
        PagedDataSource pds = new PagedDataSource();  
        //string connstring = ConfigurationManager.ConnectionStrings["pconn"].ConnectionString;  
       // SqlConnection con = new SqlConnection(connstring);  
       // DataSet ds = new DataSet();  
       //SqlDataAdapter sda = new SqlDataAdapter("select * from authors",con);  
       // sda.Fill(ds,"name");  
       // SqlDataAdapter sda2 = new SqlDataAdapter("select * from titleauthor",con);  
       // sda2.Fill(ds,"title");  
       // ds.Relations.Add("myrela",ds.Tables["name"].Columns["au_id"],ds.Tables["title"].Columns["au_id"]);   
       // pds.DataSource = ds.Tables["name"].DefaultView;  
       //Comments类是Diary类的一对多的关联关系
       Diary Diary = session.FindObject<Diary>(CriteriaOperator.Parse("Oid=?", 1));  
       pds.DataSource = Diary.Comments;
        pds.AllowPaging = true;//允许分页  
        pds.PageSize = 5;//单页显示项数  
        pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);  
        return pds;  
    }  
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)  
    {  
        if (e.Item.ItemType == ListItemType.Footer)  
        {  
            DropDownList ddlp = (DropDownList)e.Item.FindControl("ddlp");  
            HyperLink lpfirst = (HyperLink)e.Item.FindControl("hlfir");  
            HyperLink lpprev = (HyperLink)e.Item.FindControl("hlp");  
            HyperLink lpnext = (HyperLink)e.Item.FindControl("hln");  
            HyperLink lplast = (HyperLink)e.Item.FindControl("hlla");  
            pds().CurrentPageIndex = ddlp.SelectedIndex;  
            int n = Convert.ToInt32(pds().PageCount);//n为分页数  
            int i = Convert.ToInt32(pds().CurrentPageIndex);//i为当前页  
            Label lblpc = (Label)e.Item.FindControl("lblpc");  
            lblpc.Text = n.ToString();  
            Label lblp = (Label)e.Item.FindControl("lblp");  
            lblp.Text = Convert.ToString(pds().CurrentPageIndex + 1);  
            if (!IsPostBack)  
            {  
                for (int j = 0; j < n; j++)  
                {  
                    ddlp.Items.Add(Convert.ToString(j + 1));  
                }  
            }  
            if (i <= 0)  
            {  
                lpfirst.Enabled = false;  
                lpprev.Enabled = false;  
                lplast.Enabled = true;  
                lpnext.Enabled = true;  
            }  
            else  
            {  
                lpprev.NavigateUrl = "?page=" + (i - 1);  
            }  
            if (i >= n - 1)  
            {  
                lpfirst.Enabled = true;  
                lplast.Enabled = false;  
                lpnext.Enabled = false;  
                lpprev.Enabled = true;  
            }  
            else  
            {  
                lpnext.NavigateUrl = "?page=" + (i + 1);  
            }  
            lpfirst.NavigateUrl = "?page=0";//向本页传递参数page  
            lplast.NavigateUrl = "?page=" + (n - 1);  
            ddlp.SelectedIndex = Convert.ToInt32(pds().CurrentPageIndex);//更新下拉列表框中的当前选中页序号  
        }  
    }  
    protected void ddlp_SelectedIndexChanged(object sender, EventArgs e)  
    {//脚模板中的下拉列表框更改时激发  
        string pg=Convert.ToString((Convert.ToInt32(((DropDownList)sender).SelectedValue)-1));//获取列表框当前选中项  
        Response.Redirect("repeate.aspx?page="+pg);//页面转向  
    }  
}

其中发现的问题: 
因为DataSource的数据类型是List()数据集; 所以

  1. var data = new XPQuery<Diary>(session);
  2. pds.DataSource = data;

会报错:“无法计算未实现 ICollection 的数据源中的计数”;所以有两种改法 
1、

  1. var data = new XPQuery<Diary>(session).ToList();
  2. pds.DataSource = data;

2、

  1. var data = new XPCollection<Diary>(session);
  2. pds.DataSource = data;

其中XPQuery也可实现查询前几条数据;如前6条数据:

    1. var data = new XPQuery<Diary>(session).Take(6).ToList();
    2. pds.DataSource = data;

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:Linq to SQL 中将数字转换为字符串

下一篇:LINQ to SQL语句(1)Select查询的九种形式