六合开奖结果现场直播欢迎诸位!收藏本站注 册登 陆
常用三肖中特马:基础知识网络知识操作系统WPS网页制作数据库算法网页成功之路网络安全最新技术古典mid流行midmid背景下载中心
您现在的位置:首页 > 三肖中特马 > 文章页

asp.net C#查询SQL数据并分页的可参考代码

Admin | 2009-2-19 11:55:40 | ReadNums | 6819 | 标签 asp.net | 打印本页
     

   protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bind();
        }

    }
    private void bind()
    {
        //用SQL的 like
        string sql = "select * from w_bloglog where uid=" + Session["UID"] + " and logclass like '%" + this.DropDownList1.SelectedValue.ToString() + "%' order by LogTime desc";
        Db g = new Db();//这里是调用我自定义的一个类 你老实写 Gridview绑定就是了
        this.GridView1.DataSource = g.GetGrid(sql);
        this.GridView1.DataKeyNames=new string[]{"logid"};
        this.GridView1.DataBind();
        if (this.GridView1.Rows.Count == 0)
        {
            Response.Write("<div align='center'>没有日志记录</center>");
            this.t.Visible = false;
        }

        CurrPage.Text = "第" + Convert.ToInt32(GridView1.PageIndex + 1).ToString() + "页,共" + GridView1.PageCount + "页";
        int curpage = Convert.ToInt32(GridView1.PageIndex + 1);
        this.MoveFirst.Enabled = true;
        this.MoveLast.Enabled = true;
        this.MoveNext.Enabled = true;
        this.MovePrev.Enabled = true;
        if (curpage == 1)
        {
            this.MoveFirst.Enabled = false;//不显示第一页按钮
            this.MovePrev.Enabled = false;//不显示上一页按钮
        }
        if (curpage == GridView1.PageCount)
        {
            this.MoveNext.Enabled = false;//不显示下一页
            this.MoveLast.Enabled = false;//不显示最后一页
        }
 
    }
    //自定义分页
    protected void PagerChange(object sender, CommandEventArgs e)
    {

        switch (Convert.ToString(e.CommandArgument))
        {
            case "next":
                //  bind();
                if (GridView1.PageIndex < GridView1.PageCount - 1)
                    GridView1.PageIndex = GridView1.PageIndex + 1;

                bind();
                break;
            case "prev":
                //  bind();
                if (GridView1.PageIndex > 0)
                    GridView1.PageIndex = GridView1.PageIndex - 1;

                bind();
                break;
            case "last":
                GridView1.PageIndex = GridView1.PageCount - 1;
                bind();
                break;
            case "fir":
                GridView1.PageIndex = 0;
                bind();
                break;
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.EditIndex = -1;
        this.GridView1.PageIndex = e.NewPageIndex;//分页绑定

        bind();
    }


问题未解决:在线咨询

网友评论

(访客)
内容实用原创,讲得很好。
20xx年x月x日
(站长)
有问题请在线咨询。
20xx年x月x日