asp.net Datalist控件实现分页功能
程序员文章站
2024-03-11 11:58:49
在.aspx页面里的代码复制代码 代码如下:
在.aspx页面里的代码
<asp:datalist id="datalist1" runat="server" width="976px" height="745px" borderwidth="2px" cellpadding="2" cellspacing="2" repeatcolumns="7" repeatdirection="vertical" bordercolor="#666666">
<itemtemplate >
<table >
<tr>
<td >
<a href='<%#"show.aspx?iname="+eval("iname")%>'><img src='upimage/<%#databinder.eval(container.dataitem,"iname") %>' width="120" alt="点击查看大图"/></a>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist><asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:s601connectionstring %>" selectcommand="select [iname] from [image]" ></asp:sqldatasource>
<table style="width: 976px" cellpadding="0" cellspacing="0">
<tr>
<td style="width:976px; text-align: right; font-size:12pt; height:25px;">
<asp:label id="label7" runat="server" text="当前页码为:"></asp:label>
[
<asp:label id="labpage" runat="server" text="1"></asp:label>
]
<asp:label id="label6" runat="server" text="总页码为:"></asp:label>
[
<asp:label id="labbackpage" runat="server"></asp:label>
]<asp:linkbutton id="lnkbtnone" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnone_click">第一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnup" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnup_click">上一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnnext" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnnext_click">下一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnback" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnback_click">最后一页</asp:linkbutton> </td>
</tr>
</table>
在.aspx.cs页面里
protected void page_load(object sender, eventargs e)
{
dlbind();
}
public void dlbind()
{
sqlconnection sqlcon;
string strcon = "data source=(local);database=s601;uid=sa;pwd=123456";
int curpage = convert.toint32(this.labpage.text);
pageddatasource ps = new pageddatasource();
sqlcon = new sqlconnection(strcon);
sqlcon.open();
string sqlstr = "select [iname],iid from [image]";
sqldataadapter myadapter = new sqldataadapter(sqlstr, sqlcon);
dataset ds = new dataset();
myadapter.fill(ds, "iid");
ps.datasource = ds.tables["iid"].defaultview;
ps.allowpaging = true; //是否可以分页
ps.pagesize = 35; //显示的数量
ps.currentpageindex = curpage - 1; //取得当前页的页码
this.lnkbtnup.enabled = true;
this.lnkbtnnext.enabled = true;
this.lnkbtnback.enabled = true;
this.lnkbtnone.enabled = true;
if (curpage == 1)
{
this.lnkbtnone.enabled = false;//不显示第一页按钮
this.lnkbtnup.enabled = false;//不显示上一页按钮
}
if (curpage == ps.pagecount)
{
this.lnkbtnnext.enabled = false;//不显示下一页
this.lnkbtnback.enabled = false;//不显示最后一页
}
this.labbackpage.text = convert.tostring(ps.pagecount);
this.datalist1.datasource = ps;
this.datalist1.datakeyfield = "iid";
this.datalist1.databind();
sqlcon.close();
}
protected void lnkbtnone_click(object sender, eventargs e)
{
this.labpage.text = "1";
this.dlbind();
}
protected void lnkbtnup_click(object sender, eventargs e)
{
this.labpage.text = convert.tostring(convert.toint32(this.labpage.text) - 1);
this.dlbind();
}
protected void lnkbtnnext_click(object sender, eventargs e)
{
this.labpage.text = convert.tostring(convert.toint32(this.labpage.text) + 1);
this.dlbind();
}
protected void lnkbtnback_click(object sender, eventargs e)
{
this.labpage.text = this.labbackpage.text;
this.dlbind();
}
复制代码 代码如下:
<asp:datalist id="datalist1" runat="server" width="976px" height="745px" borderwidth="2px" cellpadding="2" cellspacing="2" repeatcolumns="7" repeatdirection="vertical" bordercolor="#666666">
<itemtemplate >
<table >
<tr>
<td >
<a href='<%#"show.aspx?iname="+eval("iname")%>'><img src='upimage/<%#databinder.eval(container.dataitem,"iname") %>' width="120" alt="点击查看大图"/></a>
</td>
</tr>
</table>
</itemtemplate>
</asp:datalist><asp:sqldatasource id="sqldatasource1" runat="server" connectionstring="<%$ connectionstrings:s601connectionstring %>" selectcommand="select [iname] from [image]" ></asp:sqldatasource>
<table style="width: 976px" cellpadding="0" cellspacing="0">
<tr>
<td style="width:976px; text-align: right; font-size:12pt; height:25px;">
<asp:label id="label7" runat="server" text="当前页码为:"></asp:label>
[
<asp:label id="labpage" runat="server" text="1"></asp:label>
]
<asp:label id="label6" runat="server" text="总页码为:"></asp:label>
[
<asp:label id="labbackpage" runat="server"></asp:label>
]<asp:linkbutton id="lnkbtnone" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnone_click">第一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnup" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnup_click">上一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnnext" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnnext_click">下一页</asp:linkbutton>
<asp:linkbutton id="lnkbtnback" runat="server" font-underline="false" forecolor="red"
onclick="lnkbtnback_click">最后一页</asp:linkbutton> </td>
</tr>
</table>
在.aspx.cs页面里
复制代码 代码如下:
protected void page_load(object sender, eventargs e)
{
dlbind();
}
public void dlbind()
{
sqlconnection sqlcon;
string strcon = "data source=(local);database=s601;uid=sa;pwd=123456";
int curpage = convert.toint32(this.labpage.text);
pageddatasource ps = new pageddatasource();
sqlcon = new sqlconnection(strcon);
sqlcon.open();
string sqlstr = "select [iname],iid from [image]";
sqldataadapter myadapter = new sqldataadapter(sqlstr, sqlcon);
dataset ds = new dataset();
myadapter.fill(ds, "iid");
ps.datasource = ds.tables["iid"].defaultview;
ps.allowpaging = true; //是否可以分页
ps.pagesize = 35; //显示的数量
ps.currentpageindex = curpage - 1; //取得当前页的页码
this.lnkbtnup.enabled = true;
this.lnkbtnnext.enabled = true;
this.lnkbtnback.enabled = true;
this.lnkbtnone.enabled = true;
if (curpage == 1)
{
this.lnkbtnone.enabled = false;//不显示第一页按钮
this.lnkbtnup.enabled = false;//不显示上一页按钮
}
if (curpage == ps.pagecount)
{
this.lnkbtnnext.enabled = false;//不显示下一页
this.lnkbtnback.enabled = false;//不显示最后一页
}
this.labbackpage.text = convert.tostring(ps.pagecount);
this.datalist1.datasource = ps;
this.datalist1.datakeyfield = "iid";
this.datalist1.databind();
sqlcon.close();
}
protected void lnkbtnone_click(object sender, eventargs e)
{
this.labpage.text = "1";
this.dlbind();
}
protected void lnkbtnup_click(object sender, eventargs e)
{
this.labpage.text = convert.tostring(convert.toint32(this.labpage.text) - 1);
this.dlbind();
}
protected void lnkbtnnext_click(object sender, eventargs e)
{
this.labpage.text = convert.tostring(convert.toint32(this.labpage.text) + 1);
this.dlbind();
}
protected void lnkbtnback_click(object sender, eventargs e)
{
this.labpage.text = this.labbackpage.text;
this.dlbind();
}
上一篇: Nmap和Zenmap详解
下一篇: PHP后端银联支付及退款实例代码
推荐阅读
-
asp.net Datalist控件实现分页功能
-
php基于SQLite实现的分页功能示例
-
asp.net jQuery Ajax用户登录功能的实现
-
asp.net 简单实现禁用或启用页面中的某一类型的控件
-
asp.net 使用ObjectDataSource控件在ASP.NET中实现Ajax真分页
-
使用hibernate和struts2实现分页功能的示例
-
asp.net MVC实现简单的上传功能
-
asp.net使用for循环实现Datalist的分列显示功能
-
ASP.Net PlaceHolder、Panel等控件未实现INamingContainer,导致FindControl无效
-
springmvc4+hibernate4分页查询功能实现