zoukankan      html  css  js  c++  java
  • asp.net listview 实现分页浏览效果

    页面代码:

     1 <div style="margin-top:0px;">共<asp:Label ID="lb_count" runat="server" Text ="Label"></asp:Label>条记录
     2                      共<asp:Label ID="lb_page" runat="server" Text="Label"></asp:Label>页&nbsp;
     3                      当前第<asp:Label ID="lb_CurrentPage" runat="server" Text="1"></asp:Label> 4                     <br />
     5                      <asp:LinkButton ID="LinkFirst" runat="server" OnClick="LinkFirst_Click"> 第一页
     6                      </asp:LinkButton>
     7                      <asp:LinkButton ID="LinkUp" runat="server" OnClick="LinkUp_Click"> 上一页
     8                      </asp:LinkButton>
     9                      <asp:LinkButton ID="LinkDown" runat="server" OnClick="LinkDown_Click"> 下一页
    10                      </asp:LinkButton>
    11                      <asp:LinkButton ID="LinkLast" runat="server" OnClick="LinkLast_Click"> 最后一页
    12                      </asp:LinkButton>转到第<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" AutoPostBack="True">
    13                      </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 = 10; //每页显示的数据的行数
                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();
                    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) + 1);
                    DropDownList1.SelectedValue = lb_CurrentPage.Text;
                    getArticle();
                }
                catch
                {
                    Response.Write("<script language=javascript>" + "alert(\"已经是最后一页\")" + "</script>");
                    lb_CurrentPage.Text = "1";
                    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) > 1)
                    {
                        lb_CurrentPage.Text =
                        Convert.ToString(Convert.ToInt32(lb_CurrentPage.Text) - 1);
                        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 != "1")
                {
                    lb_CurrentPage.Text = "1";
                }
                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();
            }
  • 相关阅读:
    Real-Time SQL Monitoring
    MySQL数据复制的校验
    Mysql复制-Slave库设置复制延迟
    MySQL Replication的相关文件
    mysql 启动和关闭外键约束
    mysql写注释的几种方法
    salt更换新key
    saltstack之syndic的配置
    salt-ssh的批量脚本及使用方法
    koan重装system
  • 原文地址:https://www.cnblogs.com/lihuazou/p/3727082.html
Copyright © 2011-2022 走看看