关闭

ASP.NET DataList 分页之PagedDataSource

标签: 分页asp.netPagedDataSourcedatasetdatabase
711人阅读 评论(0) 收藏 举报
分类:

第一次发文章,请大家多多包含,后面会陆续发出其他分页方式。

DataList控件的灵活性简直无可挑剔了,但是没有把分页像Gridview那样封装进去,下面和大家一起分享一下用 PagedDataSource的分页....

前台代码:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DataList.aspx.cs" Inherits="DataList" %>

<!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 style="text-align: center">
        <asp:DataList ID="DataList1" runat="server"   RepeatColumns="1"
            Width="190px">
            <ItemTemplate>
                <table border="1" bordercolor="blue" style="width: 721px">
                    <tr>
                        <td style="width: 100px; text-align: right;">
                BookMarkID:</td>
                        <td style="width: 100px">
                <asp:Label ID="BookMarkIDLabel" runat="server" Text='<%# Eval("BookMarkID") %>'>
                </asp:Label></td>
                    </tr>
                    <tr>
                        <td style="width: 100px; text-align: right;">
                BookMarkName:
                        </td>
                        <td style="width: 100px">
                <asp:Label ID="BookMarkNameLabel" runat="server" Text='<%# Eval("BookMarkName") %>'>
                </asp:Label></td>
                    </tr>
                    <tr>
                        <td style="width: 100px; text-align: right;">
                BookMarkDesc:
                        </td>
                        <td style="width: 100px">
                <asp:Label ID="BookMarkDescLabel" runat="server" Text='<%# Eval("BookMarkDesc") %>'>
                </asp:Label></td>
                    </tr>
                    <tr>
                        <td style="width: 100px; text-align: right;">
                BookMarkText:</td>
                        <td style="width: 100px">
                <asp:Label ID="BookMarkTextLabel" runat="server" Text='<%# Eval("BookMarkText") %>'>
                </asp:Label></td>
                    </tr>
                </table>
                <br />
            </ItemTemplate>
        </asp:DataList>
                第<asp:Label ID="Label1" runat="server"></asp:Label>页 
        <asp:HyperLink ID="HyperLink1" runat="server" OnDataBinding="Page_Load">首页</asp:HyperLink>
        <asp:HyperLink ID="HyperLink2" runat="server" OnDataBinding="Page_Load">上一页</asp:HyperLink>
        <asp:HyperLink ID="HyperLink3" runat="server" OnDataBinding="Page_Load">下一页</asp:HyperLink>
        <asp:HyperLink ID="HyperLink4" runat="server" OnDataBinding="Page_Load">末页</asp:HyperLink> 
        共<asp:Label
                    ID="Label2" runat="server"></asp:Label>页
    
    </div>
    </form>
</body>
</html>


 

后台代码:

using System;
using System.Data;
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;
using System.Data.SqlClient;
public partial class DataList : System.Web.UI.Page
{
   
    SqlConnection sqlcon = new SqlConnection("server=.;Database=DBDemo;user id=sa;pwd=123");
    public string strsql = "SELECT * FROM BookMarks";
    protected void Page_Load(object sender, EventArgs e)
    { //设置数据源
        PagedDataSource page = new PagedDataSource();
        SqlDataAdapter adp = new SqlDataAdapter(strsql, sqlcon);
        sqlcon.Open();
        DataSet dst = new DataSet();
        adp.Fill(dst, "table");
        DataTable tab = new DataTable();
        tab=dst.Tables["table"];
        page.DataSource = tab.DefaultView;
        //分页实现
        page.PageSize = 3;
        page.AllowPaging = true;
        int CurPage;
        if (Request.QueryString["Page"] != null)
        {
            CurPage = Int32.Parse(Request.QueryString["Page"]);
        }
        else
            CurPage = 1;
        page.CurrentPageIndex = CurPage - 1;
        this.Label1.Text = CurPage.ToString();
        this.Label2.Text = page.PageCount.ToString();
        if (!page.IsFirstPage && CurPage > 1)
        {
            this.HyperLink2.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage - 1);
        }
        if (!page.IsLastPage && CurPage < page.PageCount)
        {
            this.HyperLink3.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(CurPage + 1);
        }
        this.HyperLink1.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=1";
        this.HyperLink4.NavigateUrl = Request.CurrentExecutionFilePath + "?Page=" + Convert.ToString(page.PageCount.ToString());

        DataList1.DataSource = page;
        DataList1.DataBind();
        sqlcon.Close();
    }
    

   
}



 

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:48011次
    • 积分:866
    • 等级:
    • 排名:千里之外
    • 原创:35篇
    • 转载:28篇
    • 译文:0篇
    • 评论:4条