DataList排版技巧及自定义分页样式。
效果:
aspx.cs代码:
using
System;
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
...{
myDataList.DataSource = pds();
myDataList.DataBind();
}
}
private PagedDataSource pds()
...{
//try
// {
SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from [book]", myconn);
DataSet ds = new DataSet();
da.Fill(ds, "book");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["book"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 4;
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
//}
//catch (Exception ex)
//{
// Response.Write(ex);
//}
return pds;
}
protected void myDataList_ItemDataBound(object sender, DataListItemEventArgs e)
...{
if (e.Item.ItemType == ListItemType.Footer)
...{//判断当前项是页脚模板
int n = pds().PageCount;//将分页总数赋给变量n
int i = pds().CurrentPageIndex;//将当前分页码赋给i
Label lblpc = (Label)e.Item.FindControl("lblpc");
lblpc.Text = n.ToString();
//找到lblpc这个Label,将总页码赋给他
Label lblp = (Label)e.Item.FindControl("lblp");
lblp.Text = Convert.ToString(i + 1);
//找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
Label lblper = (Label)e.Item.FindControl("lblper");
lblper.Text = Convert.ToString(pds().PageSize);
HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
hlfir.NavigateUrl = "?page=0";
HyperLink hlla = (HyperLink)e.Item.FindControl("hlla");
hlla.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
//找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
HyperLink hln = (HyperLink)e.Item.FindControl("hln");
//找到表示上页和下页这两个控件
if (i <= 0)
...{//如果当前页已经是第0页
hlfir.Enabled = false;
hlp.Enabled = false;
hln.Enabled = true;
}
else
...{
hlp.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
}
if (i > n - 2)
...{//如果当前项已经是最末页
hln.Enabled = false;
hlla.Enabled = false;
hlp.Enabled = true;
}
else
...{
hln.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
}
DropDownList ddlpage = (DropDownList)e.Item.FindControl("ddlpage");
for (int p = 0; p < n; p++)
...{
ddlpage.Items.Add(Convert.ToString(p + 1));
if (p == i)
...{
ddlpage.SelectedIndex = p;
}
}
}
}
protected void ddl_SelectedChanged(object sender, EventArgs e)
...{
DropDownList ddl = (DropDownList)sender;
Page.Response.Redirect("?page=" +(Convert.ToInt32(ddl.SelectedItem.Value)-1));
}
}
using System.Data;
using System.Configuration;
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 _Default : System.Web.UI.Page
... {
protected void Page_Load(object sender, EventArgs e)
...{
if (!IsPostBack)
...{
myDataList.DataSource = pds();
myDataList.DataBind();
}
}
private PagedDataSource pds()
...{
//try
// {
SqlConnection myconn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter("select * from [book]", myconn);
DataSet ds = new DataSet();
da.Fill(ds, "book");
PagedDataSource pds = new PagedDataSource();
pds.DataSource = ds.Tables["book"].DefaultView;
pds.AllowPaging = true;
pds.PageSize = 4;
pds.CurrentPageIndex = Convert.ToInt32(Request.QueryString["page"]);
//}
//catch (Exception ex)
//{
// Response.Write(ex);
//}
return pds;
}
protected void myDataList_ItemDataBound(object sender, DataListItemEventArgs e)
...{
if (e.Item.ItemType == ListItemType.Footer)
...{//判断当前项是页脚模板
int n = pds().PageCount;//将分页总数赋给变量n
int i = pds().CurrentPageIndex;//将当前分页码赋给i
Label lblpc = (Label)e.Item.FindControl("lblpc");
lblpc.Text = n.ToString();
//找到lblpc这个Label,将总页码赋给他
Label lblp = (Label)e.Item.FindControl("lblp");
lblp.Text = Convert.ToString(i + 1);
//找到lblp这个Label,将当前页码赋给他,但是注意,因为页码从0开始,这里要直观的话就得加1
Label lblper = (Label)e.Item.FindControl("lblper");
lblper.Text = Convert.ToString(pds().PageSize);
HyperLink hlfir = (HyperLink)e.Item.FindControl("hlfir");
hlfir.NavigateUrl = "?page=0";
HyperLink hlla = (HyperLink)e.Item.FindControl("hlla");
hlla.NavigateUrl = "?page=" + Convert.ToInt32(n - 1);
//找到表示最前页和末页的Label,为他们的NavigateUrl属性赋为第0页和最大页码减1
HyperLink hlp = (HyperLink)e.Item.FindControl("hlp");
HyperLink hln = (HyperLink)e.Item.FindControl("hln");
//找到表示上页和下页这两个控件
if (i <= 0)
...{//如果当前页已经是第0页
hlfir.Enabled = false;
hlp.Enabled = false;
hln.Enabled = true;
}
else
...{
hlp.NavigateUrl = "?page=" + Convert.ToInt32(i - 1);
}
if (i > n - 2)
...{//如果当前项已经是最末页
hln.Enabled = false;
hlla.Enabled = false;
hlp.Enabled = true;
}
else
...{
hln.NavigateUrl = "?page=" + Convert.ToInt32(i + 1);
}
DropDownList ddlpage = (DropDownList)e.Item.FindControl("ddlpage");
for (int p = 0; p < n; p++)
...{
ddlpage.Items.Add(Convert.ToString(p + 1));
if (p == i)
...{
ddlpage.SelectedIndex = p;
}
}
}
}
protected void ddl_SelectedChanged(object sender, EventArgs e)
...{
DropDownList ddl = (DropDownList)sender;
Page.Response.Redirect("?page=" +(Convert.ToInt32(ddl.SelectedItem.Value)-1));
}
}
aspx代码:
<%
@ 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 runat = " server " >
< title > 无标题页 </ title >
</ head >
< body >
< form id = " form1 " runat = " server " >
< div >
< asp:DataList ID = " myDataList " runat = " server " RepeatColumns = " 2 " OnItemDataBound = " myDataList_ItemDataBound " >
< ItemTemplate >
< table cellpadding = " 10 " style = " font: 9pt 宋体 " >
< tr >
< td style = " width:1px; background-color:#eeeeef " />
< td valign = " top " align = " center " >
< img src = ' <%# DataBinder.Eval(Container.DataItem, "images") %> ' alt = " 图书信息 " />< p >
< asp:LinkButton ID = " LinkButton1 " runat = " server " > 购买此书 </ asp:LinkButton ></ p >
</ td >
< td valign = " top " >
< b > 书名: </ b ><% # DataBinder.Eval(Container.DataItem, " bookname " ) %>< br >
< b > 类别: </ b ><% # DataBinder.Eval(Container.DataItem, " style " ) %>< br >
< b > 出版商 ID: </ b ><% # DataBinder.Eval(Container.DataItem, " publisher " ) %>< br >
< b > 出版时间: </ b ><% # DataBinder.Eval(Container.DataItem, " publishtime " ) %>< br >
< b > 作者: </ b ><% # DataBinder.Eval(Container.DataItem, " author " ) %>
< p >
说明: <% # DataBinder.Eval(Container.DataItem, " introduction " ) %>
</ a >
</ td >
</ tr >
< tr >< td colspan = " 3 " style = " height:1px; background-color:red " ></ td ></ tr >
</ table >
</ ItemTemplate >
< FooterTemplate >
< table width = " 100% " >< tr >
< td style = " font-size: 9pt; color: #000000; background-color: #fcd699; " align = " center " > 共 < asp:Label ID = " lblpc " runat = " server " Text = " Label " ></ asp:Label > 页 当前第 < asp:Label ID = " lblp " runat = " server " Text = " Label " ></ asp:Label > 页
每页显示 < asp:Label ID = " lblper " runat = " server " Text = " Label " ></ asp:Label > 项 & nbsp; & nbsp; & nbsp; & nbsp;
< asp:HyperLink ID = " hlfir " runat = " server " Text = " 首页 " ></ asp:HyperLink > & nbsp;
< asp:HyperLink ID = " hlp " runat = " server " Text = " 上一页 " ></ asp:HyperLink >& nbsp; & nbsp;
< asp:HyperLink ID = " hln " runat = " server " Text = " 下一页 " ></ asp:HyperLink >& nbsp; & nbsp;
< asp:HyperLink ID = " hlla " runat = " server " Text = " 末页 " ></ asp:HyperLink >& nbsp; & nbsp; & nbsp; & nbsp;
跳转到 < asp:DropDownList ID = " ddlpage " runat = " server " AutoPostBack = " true " OnSelectedIndexChanged = " ddl_SelectedChanged " >
</ asp:DropDownList >
</ td >
</ tr >
</ table ></ FooterTemplate >
</ asp:DataList >
</ div >
</ form >
</ body >
</ html >
<! 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:DataList ID = " myDataList " runat = " server " RepeatColumns = " 2 " OnItemDataBound = " myDataList_ItemDataBound " >
< ItemTemplate >
< table cellpadding = " 10 " style = " font: 9pt 宋体 " >
< tr >
< td style = " width:1px; background-color:#eeeeef " />
< td valign = " top " align = " center " >
< img src = ' <%# DataBinder.Eval(Container.DataItem, "images") %> ' alt = " 图书信息 " />< p >
< asp:LinkButton ID = " LinkButton1 " runat = " server " > 购买此书 </ asp:LinkButton ></ p >
</ td >
< td valign = " top " >
< b > 书名: </ b ><% # DataBinder.Eval(Container.DataItem, " bookname " ) %>< br >
< b > 类别: </ b ><% # DataBinder.Eval(Container.DataItem, " style " ) %>< br >
< b > 出版商 ID: </ b ><% # DataBinder.Eval(Container.DataItem, " publisher " ) %>< br >
< b > 出版时间: </ b ><% # DataBinder.Eval(Container.DataItem, " publishtime " ) %>< br >
< b > 作者: </ b ><% # DataBinder.Eval(Container.DataItem, " author " ) %>
< p >
说明: <% # DataBinder.Eval(Container.DataItem, " introduction " ) %>
</ a >
</ td >
</ tr >
< tr >< td colspan = " 3 " style = " height:1px; background-color:red " ></ td ></ tr >
</ table >
</ ItemTemplate >
< FooterTemplate >
< table width = " 100% " >< tr >
< td style = " font-size: 9pt; color: #000000; background-color: #fcd699; " align = " center " > 共 < asp:Label ID = " lblpc " runat = " server " Text = " Label " ></ asp:Label > 页 当前第 < asp:Label ID = " lblp " runat = " server " Text = " Label " ></ asp:Label > 页
每页显示 < asp:Label ID = " lblper " runat = " server " Text = " Label " ></ asp:Label > 项 & nbsp; & nbsp; & nbsp; & nbsp;
< asp:HyperLink ID = " hlfir " runat = " server " Text = " 首页 " ></ asp:HyperLink > & nbsp;
< asp:HyperLink ID = " hlp " runat = " server " Text = " 上一页 " ></ asp:HyperLink >& nbsp; & nbsp;
< asp:HyperLink ID = " hln " runat = " server " Text = " 下一页 " ></ asp:HyperLink >& nbsp; & nbsp;
< asp:HyperLink ID = " hlla " runat = " server " Text = " 末页 " ></ asp:HyperLink >& nbsp; & nbsp; & nbsp; & nbsp;
跳转到 < asp:DropDownList ID = " ddlpage " runat = " server " AutoPostBack = " true " OnSelectedIndexChanged = " ddl_SelectedChanged " >
</ asp:DropDownList >
</ td >
</ tr >
</ table ></ FooterTemplate >
</ asp:DataList >
</ div >
</ form >
</ body >
</ html >