asp中查询sqll中某一个数据表的数据,要求分页显示,如:上一页,下一页。
若以下回答无法解决问题,邀请你更新回答
1个回答
展开全部
使用PagedDataSource实现BookList分页、排序功能
代码
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 MyBookShopBLL;
public partial class BookList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//首次加载,赋初值
ViewState["Page"] = 0;
ViewState["OrderBy"] = "";
if (Request.QueryString["CategoryId"] != null)
{
ViewState["CategoryId"] = Request.QueryString["CategoryId"];
}
else
{
ViewState["CategoryId"] = -1;
}
BooksDataBind();
}
}
//页面数据加载
private void BooksDataBind()
{
PagedDataSource pdsBooks = new PagedDataSource();
//对页面相关属性赋值
pdsBooks.DataSource = BookManager.GetBooksByCategoryId(Convert.ToInt32(ViewState["CategoryId"]),ViewState["OrderBy"].ToString());
pdsBooks.AllowPaging = true;
pdsBooks.PageSize = 4;
pdsBooks.CurrentPageIndex = (int)ViewState["Page"];
this.lblCurrentPage.Text = "第" + (pdsBooks.CurrentPageIndex + 1).ToString() + "页,共" + pdsBooks.PageCount.ToString() + "页";
//设置翻页按钮是否可用
btnPrev.Enabled = true;
btnNext.Enabled = true;
if (pdsBooks.IsFirstPage) btnPrev.Enabled = false;
if (pdsBooks.IsLastPage) btnNext.Enabled = false;
//绑定页面数据
this.dlBooks.DataSource = pdsBooks;
this.dlBooks.DataBind();
}
//截断字符串
public static string GetCut(string content, int num)
{
if (content.Length > num - 3)
{
return content.Substring(0, num - 3) + "…";
}
else
{
return content;
}
}
//下一页
protected void btnNext_Click(object sender, EventArgs e)
{
ViewState["Page"] = (int)ViewState["Page"]+1;
BooksDataBind();
}
//上一页
protected void btnPrev_Click(object sender, EventArgs e)
{
ViewState["Page"] = (int)ViewState["Page"] -1;
BooksDataBind();
}
protected void btnDate_Click(object sender, EventArgs e)
{
ViewState["OrderBy"] = "PublishDate";
ViewState["Page"] = 0;
btnDate.Enabled = false;
btnPrice.Enabled = true;
BooksDataBind();
}
protected void btnPrice_Click(object sender, EventArgs e)
{
ViewState["OrderBy"] = "UnitPrice";
ViewState["Page"] = 0;
btnPrice.Enabled = false;
btnDate.Enabled = true;
BooksDataBind();
}
}
页面代码
<div >
<div id="divOrder">
<div style="text-align:left;margin:20px 0 20px 0;">排序方式:
<asp:Button ID="btnDate" runat="server" Text="出版日期" OnClick="btnDate_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px" />
| <asp:Button ID="btnPrice" runat="server" Text="价格" OnClick="btnPrice_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px" /></div>
</div>
</div>
<div >
<asp:DataList ID="dlBooks" runat="server">
<ItemTemplate>
<table>
<tr>
<td rowspan="2">
<a href="BookDetail.aspx?bid=<%# Eval("Id")%>">
<img style="CURSOR: hand" height="121" alt="<%# Eval("Title") %>" src="Images/BookCovers/<%# Eval("ISBN").ToString()%>.jpg" width="95" />
</a>
</td>
<td style="FONT-SIZE: small; COLOR: red;width:650">
<a href="BookDetail.aspx?bid=<%# Eval("Id")%>" title="link_prd_name" target="_blank" class="booktitle" id="link_prd_name"><%# Eval("Title")%></a>
</td>
</tr>
<tr>
<td align="left">
<span style="font-size:12px;line-height:20px;"><%# Eval("Author") %></span><br />
<br />
<span style="font-size:12px;line-height:20px;"><%# GetCut(Eval("ContentDescription").ToString(),200) %></span>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<span style="font-size:13px;line-height:20px;font-weight:bold;"> ¥ <%# Eval("UnitPrice") %></span>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
</div>
<div style="text-align:left;margin:20px 0 20px 0;">
<asp:Label runat="server" ID="lblCurrentPage"></asp:Label>
<asp:Button ID="btnPrev" runat="server" Text="上一页" OnClick="btnPrev_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px"/>
<asp:Button ID="btnNext" runat="server" Text="下一页" OnClick="btnNext_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px"/>
</div>
代码
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 MyBookShopBLL;
public partial class BookList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//首次加载,赋初值
ViewState["Page"] = 0;
ViewState["OrderBy"] = "";
if (Request.QueryString["CategoryId"] != null)
{
ViewState["CategoryId"] = Request.QueryString["CategoryId"];
}
else
{
ViewState["CategoryId"] = -1;
}
BooksDataBind();
}
}
//页面数据加载
private void BooksDataBind()
{
PagedDataSource pdsBooks = new PagedDataSource();
//对页面相关属性赋值
pdsBooks.DataSource = BookManager.GetBooksByCategoryId(Convert.ToInt32(ViewState["CategoryId"]),ViewState["OrderBy"].ToString());
pdsBooks.AllowPaging = true;
pdsBooks.PageSize = 4;
pdsBooks.CurrentPageIndex = (int)ViewState["Page"];
this.lblCurrentPage.Text = "第" + (pdsBooks.CurrentPageIndex + 1).ToString() + "页,共" + pdsBooks.PageCount.ToString() + "页";
//设置翻页按钮是否可用
btnPrev.Enabled = true;
btnNext.Enabled = true;
if (pdsBooks.IsFirstPage) btnPrev.Enabled = false;
if (pdsBooks.IsLastPage) btnNext.Enabled = false;
//绑定页面数据
this.dlBooks.DataSource = pdsBooks;
this.dlBooks.DataBind();
}
//截断字符串
public static string GetCut(string content, int num)
{
if (content.Length > num - 3)
{
return content.Substring(0, num - 3) + "…";
}
else
{
return content;
}
}
//下一页
protected void btnNext_Click(object sender, EventArgs e)
{
ViewState["Page"] = (int)ViewState["Page"]+1;
BooksDataBind();
}
//上一页
protected void btnPrev_Click(object sender, EventArgs e)
{
ViewState["Page"] = (int)ViewState["Page"] -1;
BooksDataBind();
}
protected void btnDate_Click(object sender, EventArgs e)
{
ViewState["OrderBy"] = "PublishDate";
ViewState["Page"] = 0;
btnDate.Enabled = false;
btnPrice.Enabled = true;
BooksDataBind();
}
protected void btnPrice_Click(object sender, EventArgs e)
{
ViewState["OrderBy"] = "UnitPrice";
ViewState["Page"] = 0;
btnPrice.Enabled = false;
btnDate.Enabled = true;
BooksDataBind();
}
}
页面代码
<div >
<div id="divOrder">
<div style="text-align:left;margin:20px 0 20px 0;">排序方式:
<asp:Button ID="btnDate" runat="server" Text="出版日期" OnClick="btnDate_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px" />
| <asp:Button ID="btnPrice" runat="server" Text="价格" OnClick="btnPrice_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px" /></div>
</div>
</div>
<div >
<asp:DataList ID="dlBooks" runat="server">
<ItemTemplate>
<table>
<tr>
<td rowspan="2">
<a href="BookDetail.aspx?bid=<%# Eval("Id")%>">
<img style="CURSOR: hand" height="121" alt="<%# Eval("Title") %>" src="Images/BookCovers/<%# Eval("ISBN").ToString()%>.jpg" width="95" />
</a>
</td>
<td style="FONT-SIZE: small; COLOR: red;width:650">
<a href="BookDetail.aspx?bid=<%# Eval("Id")%>" title="link_prd_name" target="_blank" class="booktitle" id="link_prd_name"><%# Eval("Title")%></a>
</td>
</tr>
<tr>
<td align="left">
<span style="font-size:12px;line-height:20px;"><%# Eval("Author") %></span><br />
<br />
<span style="font-size:12px;line-height:20px;"><%# GetCut(Eval("ContentDescription").ToString(),200) %></span>
</td>
</tr>
<tr>
<td align="right" colspan="2">
<span style="font-size:13px;line-height:20px;font-weight:bold;"> ¥ <%# Eval("UnitPrice") %></span>
</td>
</tr>
</table>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
</div>
<div style="text-align:left;margin:20px 0 20px 0;">
<asp:Label runat="server" ID="lblCurrentPage"></asp:Label>
<asp:Button ID="btnPrev" runat="server" Text="上一页" OnClick="btnPrev_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px"/>
<asp:Button ID="btnNext" runat="server" Text="下一页" OnClick="btnNext_Click" BackColor="#C0FFC0" BorderColor="SeaGreen" BorderStyle="Solid" BorderWidth="1px" CssClass="anniu" Font-Size="12px" ForeColor="Black" Font-Bold="False" Height="16px" Width="57px"/>
</div>
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询