ASP.NET DataList控件分页功能
2013-03-20 12:52
501 查看
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!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 id="Head1" runat="server"> <title></title> <style type="text/css"> .style3 { width: 19px; } .style5 { width: 157px; } .style7 { width: 133px; } .style8 { width: 294px; } </style> </head> <body> <form id="form1" runat="server"> <div> <table border="0" cellpadding="0" cellspacing="0" style="width: 653px"> <tr> <td align="left"> <asp:DataList ID="DataList1" runat="server" style="margin-right: 5px" Width="575px" onitemcommand="DataList1_ItemCommand" onitemdatabound="DataList1_ItemDataBound" BackColor="LightGoldenrodYellow" BorderColor="Tan" BorderWidth="1px" CellPadding="2" ForeColor="Black"> <HeaderStyle BackColor="Tan" Font-Bold="True" /> <ItemTemplate> <table> <tr style="border-bottom-style: groove; border-bottom-width: medium; border-bottom-color: #FFFFFF"> <td rowspan="3" align="center" class="style3"> <a href='#'> <img border="0" height="80" src='images/showimg.gif' width="80"> </img> </a> </td> <td align="left" class="style8"> <asp:Image ID="Image4" runat="server" ImageUrl="~/images/ico2.gif" /> <a><%#Eval("bg_name")%></a> </td> <td align="left" class="style7"> </td> <td class="style5"> </td> </tr> <tr> <td align="left" class="style8"> 空间主人:<a><%#Eval("bg_name") %></a></td> <td align="left" class="style7"> 创建时间:<a><%#Eval("bg_createtime","{0:D}") %></a></td> <td class="style5"> </td> </tr> <tr> <td align="left" colspan="3"> 个性签名:<a ><%#Eval("bg_p_autograph").ToString().Length > 20 ? Eval("bg_p_autograph").ToString().Substring(0, 20) + "..." : Eval("bg_p_autograph")%></a></td> </tr> </table> </ItemTemplate> <AlternatingItemStyle BackColor="PaleGoldenrod" /> <FooterStyle BackColor="Tan" /> <FooterTemplate> <div style="text-align: center"> <table id="Page" border="1" cellpadding="0" cellspacing="0" style="font-size: 12px; width: 68%"> <tr> <td > <asp:Label ID="labCurrentPage" runat="server"></asp:Label>/ <asp:Label ID="labPageCount" runat="server"></asp:Label> <asp:LinkButton ID="lnkbtnFirst" runat="server" CommandName="first" Font-Underline="False" ForeColor="Black">首页</asp:LinkButton> <asp:LinkButton ID="lnkbtnFront" runat="server" CommandName="pre" Font-Underline="False" ForeColor="Black">上一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnNext" runat="server" CommandName="next" Font-Underline="False" ForeColor="Black">下一页</asp:LinkButton> <asp:LinkButton ID="lnkbtnLast" runat="server" CommandName="last" Font-Underline="False" ForeColor="Black">尾页</asp:LinkButton> 跳转至:<asp:TextBox ID="txtPage" runat="server" Width="35px" Height="21px"></asp:TextBox> <asp:Button ID="Button1" runat="server" CommandName="search" Text="GO" Height="19px" /> <br /> </td> </tr> </table> </div> </FooterTemplate> <SelectedItemStyle BackColor="DarkSlateBlue" ForeColor="GhostWhite" /> </asp:DataList> </td> </tr> </table> </div> </form> </body> </html>
后台代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class _Default : System.Web.UI.Page
{
protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//调用自定义方法绑定数据到控件(为以后做MVC打下基础)
BindDataList(0);
}
}
//对datelist进行数据绑定
private void BindDataList(int currentpage)
{
pds.AllowPaging = true;//允许分页
pds.PageSize = 3;//每页显示3条数据
pds.CurrentPageIndex = currentpage;//当前页为传入的一个int型值
//这里将数据库连接字符串写在web.config文件中,通过这个语句来调用,这样方便对连接字符串的修改
string connStr = ConfigurationManager.ConnectionStrings["connStr"].ConnectionString;
//创建数据库连接对象
SqlConnection con = new SqlConnection(connStr);
//定义查询语句,这里最好将SQL语句在SQL中写好并验证正确确在复制粘贴过来(在对数据查询时最好只查所需的一些不需要的数据就不要取出,这样可以提高运行的效率)
string strSql = "SELECT * FROM bg_spatial";//定义一条SQL语句
con.Open();//打开数据库连接 (当然此句可以不写的)
SqlDataAdapter sda = new SqlDataAdapter(strSql, con);
DataSet ds = new DataSet();
sda.Fill(ds);//把执行得到的数据放在数据集中
pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中
DataList1.DataSource = pds;//绑定Datalist
DataList1.DataBind();
con.Close();
}
protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
switch (e.CommandName)
{
//以下5个为 捕获用户点击 上一页 下一页等时发生的事件
case "first"://第一页
pds.CurrentPageIndex = 0;
BindDataList(pds.CurrentPageIndex);
break;
case "pre"://上一页
pds.CurrentPageIndex = pds.CurrentPageIndex - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "next"://下一页
pds.CurrentPageIndex = pds.CurrentPageIndex + 1;
BindDataList(pds.CurrentPageIndex);
break;
case "last"://最后一页
pds.CurrentPageIndex = pds.PageCount - 1;
BindDataList(pds.CurrentPageIndex);
break;
case "search"://页面跳转页
if (e.Item.ItemType == ListItemType.Footer)
{
int PageCount = int.Parse(
a132
pds.PageCount.ToString());
TextBox txtPage = e.Item.FindControl("txtPage") as TextBox;
int MyPageNum = 0;
if (!txtPage.Text.Equals(""))
MyPageNum = Convert.ToInt32(txtPage.Text.ToString());
if (MyPageNum <= 0 || MyPageNum > PageCount)
{
Response.Write("<script>alert('请输入页数并确定没有超出总页数!')</script>");
txtPage.Text = "";
}
else
BindDataList(MyPageNum - 1);
}
break;
}
}
protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
//以下六个为得到脚模板中的控件,并创建变量.
Label CurrentPage = e.Item.FindControl("labCurrentPage") as Label;
Label PageCount = e.Item.FindControl("labPageCount") as Label;
LinkButton FirstPage = e.Item.FindControl("lnkbtnFirst") as LinkButton;
LinkButton PrePage = e.Item.FindControl("lnkbtnFront") as LinkButton;
LinkButton NextPage = e.Item.FindControl("lnkbtnNext") as LinkButton;
LinkButton LastPage = e.Item.FindControl("lnkbtnLast") as LinkButton;
CurrentPage.Text = (pds.CurrentPageIndex + 1).ToString();//绑定显示当前页
PageCount.Text = pds.PageCount.ToString();//绑定显示总页数
if (pds.IsFirstPage)//如果是第一页,首页和上一页不能用
{
FirstPage.Enabled = false;
PrePage.Enabled = false;
}
if (pds.IsLastPage)//如果是最后一页"下一页"和"尾页"按钮不能用
{
NextPage.Enabled = false;
LastPage.Enabled = false;
}
}
}
}
相关文章推荐
- asp.net Datalist控件实现分页功能
- asp.net Datalist控件实现分页功能
- asp.net控件DataList分页用法
- Studio for ASP.NET Wijmo:使用 C1Pager 对 DataList 控件分页
- asp.net DataList控件分页代码
- 如何让DataList具有分页的功能(asp.net)
- ASP.NET - 自定义控件【第十天 实现分页功能的DataList(附该系列文章PDF版本)】
- ASP.NET中Repeater控件实现分页功能
- asp.net控件DataList分页用法
- 推荐功能齐全的Asp.net MVC 分页控件!
- ASP.NET中Repeater控件实现分页功能
- 用DataList控件和ObjectDataSource在ASP.NET 2.0中实现高效能数据分页
- ASP.NET自定义控件 第十天 实现分页功能的DataList(附该系列文章PDF版本)
- 用DataList控件和ObjectDataSource在ASP.NET 2.0中实现高效能数据分页
- asp.net DataList实现分页功能
- ASP.NET中Repeater控件实现分页功能
- 推荐功能齐全的Asp.net MVC 分页控件!
- ASP.NET 使用DataList控件实现分页显示数据
- ASP.NET:DataList控件的选择输入功能