求高手编写一个关于DataList绑定数据分页的代码。
已经对DataList绑定数据源成功。要怎么样创建上下翻页。一页只显示5个行数据。能够读出共有几页。求高手给个模版代码。在ASP.NET上用的是C#....
已经对DataList绑定数据源成功。要怎么样创建上下翻页。一页只显示5个行数据。能够读出共有几页。
求高手给个模版代码。在ASP.NET上用的是C#. 展开
求高手给个模版代码。在ASP.NET上用的是C#. 展开
2个回答
展开全部
ASPX源代码:
<divstyle="width:745px;height:10px">
共<asp:LabelID="lb_count"runat="server"Text="Label"></asp:Label>条记录
共<asp:LabelID="lb_page"runat="server"Text="Label"></asp:Label>页
当前第<asp:LabelID="lb_CurrentPage"runat="server"Text="1"></asp:Label>页
<asp:LinkButtonID="LinkFirst"runat="server"OnClick="LinkFirst_Click"> 第一页
</asp:LinkButton>
<asp:LinkButtonID="LinkUp"runat="server"OnClick="LinkUp_Click"> 上一页
</asp:LinkButton>
<asp:LinkButtonID="LinkDown"runat="server"OnClick="LinkDown_Click"> 下一页
</asp:LinkButton>
<asp:LinkButtonID="LinkLast"runat="server"OnClick="LinkLast_Click"> 最后一页
</asp:LinkButton>
转到第<asp:DropDownListID="DropDownList1"runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"AutoPostBack="True">
</asp:DropDownList>页
.aspx.cs
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 System.Data.SqlClient;
publicpartialclassshopnvzhuang : System.Web.UI.Page
{
protectedSqlConnection conn; //添加数据库的操作对象
protectedSqlDataAdapter da;
protectedDataSet ds;
protectedSqlCommand comm;
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getArticle();
}
}
privatevoid getArticle() //取得Article 数据
{
conn = newSqlConnection("Data Source=(local);Database=e-commerce;Uid=sa;Pwd=");//取连接字符串,建立连接
da = newSqlDataAdapter();
da.SelectCommand = newSqlCommand("select 商品名,价格,特价,photo from goods,secondclassify where secondclassify.二级类号=goods.二级类号 and secondclassify.一级类号=12001", conn);
ds = newDataSet();
try
{
conn.Open();
da.Fill(ds, "Article");
conn.Close();
}
catch (SqlException e1)
{
Response.Write(e1.ToString());
}
int cup = Convert.ToInt32(this.lb_CurrentPage.Text); //当前页数,初始化为地1 页
PagedDataSource ps = newPagedDataSource();
ps.DataSource = ds.Tables["Article"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 5; //每页显示的数据的行数
ps.CurrentPageIndex = cup - 1;
lb_count.Text = ps.DataSourceCount.ToString(); //获取记录总数
lb_page.Text = ps.PageCount.ToString(); //获取总页数
if (!IsPostBack)
{
for (int i = 1; i < ps.PageCount + 1; i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
LinkUp.Enabled = true;
LinkDown.Enabled = true;
}
try
{
DropDownList1.SelectedItem.Text = cup.ToString();
DataList1.DataSource = ps;
DataList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protectedvoid LinkDown_Click(object sender, EventArgs e) //下一页按钮代码
{
try
{
lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) + 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
catch
{
Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
lb_CurrentPage.Text = "1";
getArticle();
}
}
protectedvoid DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //跳转到指定页代码
{
int page = Convert.ToInt16((DropDownList1.SelectedItem.Value));
lb_CurrentPage.Text = page.ToString();
getArticle();
}
protectedvoid LinkUp_Click(object sender, EventArgs e) //上一页按钮代码
{
try
{
if (Convert.ToInt16(lb_CurrentPage.Text) > 1)
{
lb_CurrentPage.Text =
Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) - 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
else
{
Response.Write("<script language=javascript>" + "alert(\"已经是第一页\")" + "</script>");
}
}
catch
{
Response.Write("<script language=javascript>" + "alert(\" 已经是第一页\")" + "</script>");
}
}
protectedvoid LinkFirst_Click(object sender, EventArgs e) //跳到第一页代码
{
if (lb_CurrentPage.Text != "1")
{
lb_CurrentPage.Text = "1";
}
else
{
Response.Write("<script language=javascript>" + "alert(\" 已经是第一页\")" + "</script>");
}
getArticle();
}
protectedvoid LinkLast_Click(object sender, EventArgs e) //跳到最后一页代码
{
if (lb_CurrentPage.Text.ToString() != lb_page.Text.ToString())
{
lb_CurrentPage.Text = lb_page.Text.ToString();
}
else
{
Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
}
getArticle();
}
}
函数中的数据库连接要改为你自己的。。改改表名差不多就可以了。。
<divstyle="width:745px;height:10px">
共<asp:LabelID="lb_count"runat="server"Text="Label"></asp:Label>条记录
共<asp:LabelID="lb_page"runat="server"Text="Label"></asp:Label>页
当前第<asp:LabelID="lb_CurrentPage"runat="server"Text="1"></asp:Label>页
<asp:LinkButtonID="LinkFirst"runat="server"OnClick="LinkFirst_Click"> 第一页
</asp:LinkButton>
<asp:LinkButtonID="LinkUp"runat="server"OnClick="LinkUp_Click"> 上一页
</asp:LinkButton>
<asp:LinkButtonID="LinkDown"runat="server"OnClick="LinkDown_Click"> 下一页
</asp:LinkButton>
<asp:LinkButtonID="LinkLast"runat="server"OnClick="LinkLast_Click"> 最后一页
</asp:LinkButton>
转到第<asp:DropDownListID="DropDownList1"runat="server"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"AutoPostBack="True">
</asp:DropDownList>页
.aspx.cs
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 System.Data.SqlClient;
publicpartialclassshopnvzhuang : System.Web.UI.Page
{
protectedSqlConnection conn; //添加数据库的操作对象
protectedSqlDataAdapter da;
protectedDataSet ds;
protectedSqlCommand comm;
protectedvoid Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
getArticle();
}
}
privatevoid getArticle() //取得Article 数据
{
conn = newSqlConnection("Data Source=(local);Database=e-commerce;Uid=sa;Pwd=");//取连接字符串,建立连接
da = newSqlDataAdapter();
da.SelectCommand = newSqlCommand("select 商品名,价格,特价,photo from goods,secondclassify where secondclassify.二级类号=goods.二级类号 and secondclassify.一级类号=12001", conn);
ds = newDataSet();
try
{
conn.Open();
da.Fill(ds, "Article");
conn.Close();
}
catch (SqlException e1)
{
Response.Write(e1.ToString());
}
int cup = Convert.ToInt32(this.lb_CurrentPage.Text); //当前页数,初始化为地1 页
PagedDataSource ps = newPagedDataSource();
ps.DataSource = ds.Tables["Article"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = 5; //每页显示的数据的行数
ps.CurrentPageIndex = cup - 1;
lb_count.Text = ps.DataSourceCount.ToString(); //获取记录总数
lb_page.Text = ps.PageCount.ToString(); //获取总页数
if (!IsPostBack)
{
for (int i = 1; i < ps.PageCount + 1; i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
LinkUp.Enabled = true;
LinkDown.Enabled = true;
}
try
{
DropDownList1.SelectedItem.Text = cup.ToString();
DataList1.DataSource = ps;
DataList1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protectedvoid LinkDown_Click(object sender, EventArgs e) //下一页按钮代码
{
try
{
lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) + 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
catch
{
Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
lb_CurrentPage.Text = "1";
getArticle();
}
}
protectedvoid DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //跳转到指定页代码
{
int page = Convert.ToInt16((DropDownList1.SelectedItem.Value));
lb_CurrentPage.Text = page.ToString();
getArticle();
}
protectedvoid LinkUp_Click(object sender, EventArgs e) //上一页按钮代码
{
try
{
if (Convert.ToInt16(lb_CurrentPage.Text) > 1)
{
lb_CurrentPage.Text =
Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) - 1);
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
else
{
Response.Write("<script language=javascript>" + "alert(\"已经是第一页\")" + "</script>");
}
}
catch
{
Response.Write("<script language=javascript>" + "alert(\" 已经是第一页\")" + "</script>");
}
}
protectedvoid LinkFirst_Click(object sender, EventArgs e) //跳到第一页代码
{
if (lb_CurrentPage.Text != "1")
{
lb_CurrentPage.Text = "1";
}
else
{
Response.Write("<script language=javascript>" + "alert(\" 已经是第一页\")" + "</script>");
}
getArticle();
}
protectedvoid LinkLast_Click(object sender, EventArgs e) //跳到最后一页代码
{
if (lb_CurrentPage.Text.ToString() != lb_page.Text.ToString())
{
lb_CurrentPage.Text = lb_page.Text.ToString();
}
else
{
Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
}
getArticle();
}
}
函数中的数据库连接要改为你自己的。。改改表名差不多就可以了。。
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询