ASP.NET之Datalist详解(分页)
datalist控件数据源绑定方法和gridview控件基本相似,但要显示数据需要设计控件的模版
datalist分页
.apsx界面
<head 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">
<p>
<table border="0" cellpadding="0" cellspacing="0"
style="width: 653px">
<tr>
<td align="left">
<: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>
<p 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>
</p>
</footertemplate>
<selecteditemstyle backcolor="darkslateblue" forecolor="ghostwhite" />
</asp:datalist>
</td>
</tr>
</table>
</p>
</form>
</body>
</html>
核心在datalist控件的
itemtemplate和footertemplate 至于其他杂七杂八的都是做的些美工。
.cs 界面
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(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;
}
}
}
}
摘自 jory
下一篇: ASP初学者学习ASP指令
推荐阅读