对ASPnet的数据库表单分页需要写后台代码的,我这不久前刚写了个SQL的分页处理程序,希望能给你启示,原理是一样的,把语句换下就可以了,我对Oracle数据库了解甚少
前台:
<asp:DataList ID="lstProducts" runat="server" EnableViewState="false" DataKeyField="ProductID" RepeatColumns="6" Width="100%">
<ItemTemplate>
<table style="font-size: 10pt;">
<tr><td><a href='ProductDetailsaspxProductID=<%# Eval("ProductID") %>' style="color:#0e0e6c" >
<img alt="更多信息" runat="server" style="border-right: 0px solid; border-top: 0px solid; border-left: 0px solid; border-bottom: 0px solid" src='<%# "readImageaspxSize=ThumbNail&ProductID=" + Eval("ProductID") %>' id="imgThumbnail" /></a>
</td></tr>
<tr><td><%# DataBinderEval(ContainerDataItem, "Color") %></td></tr>
<tr><td><%# DataBinderEval(ContainerDataItem, "Price") %></td></tr>
<tr><td><%# DataBinderEval(ContainerDataItem, "ProductID") %></td></tr>
<tr><td><a href="AddToCartaspxProductID=<%# DataBinderEval(ContainerDataItem, "ProductID") %>">放入购物车</a></td></tr>
</table>
</ItemTemplate>
</asp:DataList>
<div style="font-size:small ; text-align:right">
<asp:HyperLink ID="lnkPrev" runat="server">上一页</asp:HyperLink>
<asp:HyperLink ID="lnkNext" runat="server">下一页</asp:HyperLink>
</div>
后台:
protected void Page_Load(object sender, EventArgs e)
{
int CurrPage = 0;
string sSql = "";
//ResponseWrite("IsPostBack: " + IsPostBackToString());
if (MasterSelectedValue == "")
sSql = "SELECT ProductID, Name, ProductNumber, Color , Price FROM Product";
else
sSql = "SELECT ProductID, Name, ProductNumber, Color , Price FROM Product" + " where ProductSubcategoryID=" + MasterSelectedValue;
//ResponseWrite("<br>"+sSql);
DataSet ds = DataAccessHelperRunQuery(sSql);
PagedDataSource pds = new PagedDataSource();
pdsDataSource = dsTables[0]DefaultView;
pdsAllowPaging = true;
pdsPageSize = 18;
if (IsPostBack)
{
CurrPage = 1;
}
else
{
if (RequestQueryString["Page"] != null)
CurrPage = ConvertToInt32(RequestQueryString["Page"]);
else
CurrPage = 1;
}
pdsCurrentPageIndex = CurrPage - 1;
//lblCurrPageText = "当前页: " + CurrPageToString();
if (CurrPage<pdsPageCount)
{
int NextPage = CurrPage + 1;
lnkNextNavigateUrl = "ProductsaspxPage=" + NextPageToString();
}
if (CurrPage > 1 && CurrPage < pdsPageCount)
{
int PrevPage = CurrPage - 1;
lnkPrevNavigateUrl = "ProductsaspxPage=" + PrevPageToString();
}
lstProductsDataSource = pds;
lstProductsDataBind();
}
aspx文件中的代码(格式你自己调):
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:LinkButton ID="lnkbtnFrist" runat="server" OnClick="lnkbtnFrist_Click">首页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnPre" runat="server" OnClick="lnkbtnPre_Click">上一页</asp:LinkButton>
<asp:Label ID="lblCurrentPage" runat="server"></asp:Label>
<asp:LinkButton ID="lnkbtnNext" runat="server" OnClick="lnkbtnNext_Click">下一页</asp:LinkButton>
<asp:LinkButton ID="lnkbtnLast" runat="server" OnClick="lnkbtnLast_Click">尾页</asp:LinkButton>
跳转到第<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>页
aspxcs文件中的代码:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
thisGridView1PageIndex = thisddlCurrentPageSelectedIndex;
bind();
}
protected void lnkbtnFrist_Click(object sender, EventArgs e)
{
thisGridView1PageIndex = 0;
bind();
}
protected void lnkbtnPre_Click(object sender, EventArgs e)
{
if (thisGridView1PageIndex > 0)
{
thisGridView1PageIndex = thisGridView1PageIndex - 1;
bind();
}
}
protected void lnkbtnNext_Click(object sender, EventArgs e)
{
if (thisGridView1PageIndex < thisGridView1PageCount)
{
thisGridView1PageIndex = thisGridView1PageIndex + 1;
bind();
}
}
protected void lnkbtnLast_Click(object sender, EventArgs e)
{
thisGridView1PageIndex = thisGridView1PageCount;
bind();
}
public void bind()
{
GridView1DataSource = getData();
GridView1DataKeyNames = new string[] { "学号" };
GridView1DataBind();
thisddlCurrentPageItemsClear();
for (int i = 1; i <= thisGridView1PageCount; i++)
{
thisddlCurrentPageItemsAdd(iToString());
}
thisddlCurrentPageSelectedIndex = thisGridView1PageIndex;
}
getdata()函数你能不能自己写?就是连接数据库,把查询出来的结果放到一个DataTable对象中,然后函数返回值就是这个DataTable对象。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)