asp.net listview 实现分页浏览效果
2024-08-28 20:16:04
页面代码:
<div style="margin-top:0px;">共<asp:Label ID="lb_count" runat="server" Text ="Label"></asp:Label>条记录
共<asp:Label ID="lb_page" runat="server" Text="Label"></asp:Label>页
当前第<asp:Label ID="lb_CurrentPage" runat="server" Text=""></asp:Label>页
<br />
<asp:LinkButton ID="LinkFirst" runat="server" OnClick="LinkFirst_Click"> 第一页
</asp:LinkButton>
<asp:LinkButton ID="LinkUp" runat="server" OnClick="LinkUp_Click"> 上一页
</asp:LinkButton>
<asp:LinkButton ID="LinkDown" runat="server" OnClick="LinkDown_Click"> 下一页
</asp:LinkButton>
<asp:LinkButton ID="LinkLast" runat="server" OnClick="LinkLast_Click"> 最后一页
</asp:LinkButton>转到第<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>页</div>
后台代码:
protected SqlDataAdapter da;
protected DataSet ds;
private void getArticle() //取得Article 数据
{
string connectionString = "Server=.;database=Flower;uid=sa;pwd=zhuwenfan";
SqlConnection myconn = new SqlConnection(connectionString);//取连接字符串,建立连接
da = new SqlDataAdapter();
da.SelectCommand = new SqlCommand("select * from FLOWERS_ORDER order by O_ID desc", myconn);
ds = new DataSet(); try
{
myconn.Open();
da.Fill(ds, "Article");
myconn.Close();
}
catch (SqlException e1)
{
Response.Write(e1.ToString());
}
int cup = Convert.ToInt32(this.lb_CurrentPage.Text); //当前页数,初始化为地1 页
PagedDataSource ps = new PagedDataSource();
ps.DataSource = ds.Tables["Article"].DefaultView;
ps.AllowPaging = true;
ps.PageSize = ; //每页显示的数据的行数
ps.CurrentPageIndex = cup - ;
lb_count.Text = ps.DataSourceCount.ToString(); //获取记录总数
lb_page.Text = ps.PageCount.ToString(); //获取总页数
if (!IsPostBack)
{
for (int i = ; i < ps.PageCount + ; i++)
{
this.DropDownList1.Items.Add(i.ToString());
}
LinkUp.Enabled = true;
LinkDown.Enabled = true;
}
try
{
DropDownList1.SelectedItem.Text = cup.ToString();
ListView1.DataSource = ps;
ListView1.DataBind();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
protected void LinkDown_Click(object sender, EventArgs e) //下一页按钮代码
{
try
{
lb_CurrentPage.Text = Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) + );
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
catch
{
Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
lb_CurrentPage.Text = "";
getArticle(); }
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) //跳转到指定页代码
{
int page = Convert.ToInt16((DropDownList1.SelectedItem.Value));
lb_CurrentPage.Text = page.ToString();
getArticle();
}
protected void LinkUp_Click(object sender, EventArgs e) //上一页按钮代码
{
try
{
if (Convert.ToInt16(lb_CurrentPage.Text) > )
{
lb_CurrentPage.Text =
Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) - );
DropDownList1.SelectedValue = lb_CurrentPage.Text;
getArticle();
}
else
{
Response.Write("<script>alert('已经是第一页');location.replace(location.href);</script>");
}
}
catch
{
Response.Write("<script>alert('已经是第一页');location.replace(location.href);</script>");
}
}
protected void LinkFirst_Click(object sender, EventArgs e) //跳到第一页代码
{
if (lb_CurrentPage.Text != "")
{
lb_CurrentPage.Text = "";
}
else
{
Response.Write("<script language=javascript>" + "alert(\" 已经是第一页\")" + "</script>");
}
getArticle();
}
protected void 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();
}
最新文章
- android studio用法笔记
- timestamp 类型的索引
- Omnet++ 4.0 入门实例教程
- photoshop:找不到增效工具入口点
- django访问静态文件
- java md5方法 for Android
- ExecuteReader: CommandText 属性尚未初始化
- linux yum命令
- 20130620—ant和java杂学随笔
- mysql CMD命令
- Chapter 5 Blood Type——32
- url路由配置以及渲染方式
- windows10安装anaconda,配置tensorflow
- Cannot change version of project facet Dynamic Web Module to 2.4问题解决
- VC++ 使用attributes定义接口
- 【Selenium2】【Shell】
- IDA 逆向工程 反汇编使用
- pipy国内镜像的网址
- 今天被坑了,而且被坑的好爽! 该死的UTF-8 有 BOM 格式编码
- linux centos7 防火墙及端口开放相关命令