我用AspNetPager与gridview结合显示数据,但是在模糊查询后AspNetPager控件就没有效果了!请问哪位高手有例子!帮忙小弟一下!

解决方案 »

  1.   

    不管是精确查询还是模糊查询,关键都是获取数据源,只要有数据就不会说控件没效果。
    <webdiyer:AspNetPager ID="AspNetPager1" runat="server" AlwaysShow="true" PageSize="20" 
                    FirstPageText="首页" NextPageText="下一页" PrevPageText="上一页" LastPageText="尾页" 
                    OnPageChanged="AspNetPager1_PageChanged" CssClass="pages">
                </webdiyer:AspNetPager>
    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AspNetPager1.RecordCount = getTotalRecore();
                bindData();
            }
        }
           private void bindData()
        {
            string strResult = "";
            string strSQL = "proc_Comm_Affairs_getList";
            using (DBOperate dbo = new DBOperate())
            {
                SqlParameter[] sqlPa = new SqlParameter[]
                {
                    DBOperate .MakeInParam ("@PageIndex",AspNetPager1 .CurrentPageIndex ),
                    DBOperate .MakeInParam ("@PageSize",AspNetPager1 .PageSize )
                };
                DataTable dt = dbo.ExecuteTable(strSQL ,sqlPa ,CommandType.StoredProcedure );            rp_NewsList.DataSource = dt;
                rp_NewsList.DataBind();
              }
        }    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            bindData();
        }    private int getTotalRecore()
        {
            int iTotalRecore = 0;
            using (DBOperate dbo = new DBOperate())
            {
                string strSQL = "select count(*) from Comm_Affairs where isShow='1' and catalogID='1'";
                iTotalRecore = Convert .ToInt32 (dbo.ExecuteScalar(strSQL ,null ,CommandType.Text ));
            }
            return iTotalRecore;
        }
      

  2.   


    public void BindGv()
        {
            string key = TextBox1.Text.Trim();        string strSqlconn = "Data Source=WANG-PC;Initial Catalog=ADOLab1;Persist Security Info=True;User ID=sa;Password=123456";
            SqlConnection sqlconn = new SqlConnection(strSqlconn);        sqlconn.Open();
            string strSql = "select * from Article where articleTitle like @key";        SqlDataAdapter sda = new SqlDataAdapter(strSql, strSqlconn);
            sda.SelectCommand.Parameters.Add(new SqlParameter("@key", "%" + key + "%"));        DataSet ds = new DataSet();
            sda.Fill(ds);        PagedDataSource pgs = new PagedDataSource();
            DataView dv;        if (ds == null)
            {
                dv = new DataView();        }
            else
            {
                dv = ds.Tables[0].DefaultView;        }        AspNetPager1.RecordCount = dv.Count;
            pgs.DataSource = dv;        pgs.AllowPaging = true;        pgs.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;
            pgs.PageSize = AspNetPager1.PageSize;        GridView1.DataSource = pgs;
            //GridView1.DataSource = ds;
            GridView1.DataBind();        sqlconn.Close();
        }按钮调用一下BindGv()就是了。
      

  3.   

    string strSql = "select * from Article where articleTitle like @key";
    改成这样
    string strSql = "select * from Article where articleTitle like @key and articleTime like @key2";
    再加一句sda.SelectCommand.Parameters.Add(new SqlParameter("@key", "%" + key2 + "%"));
    有几个框加几个就是了
    我的显示
      

  4.   

    查询后需要重新设置RecordCount的值为查询结果的总记录数,并将CurrentPageIndex设为1,同时重新绑定数据。
      

  5.   

    查询按钮:
     protected void btnSearch_Click(object sender, ImageClickEventArgs e)
            {
                Account ac = new Account();
                string sname = this.name.Text;
                string startT = this.txtstatTime.Text;
                string endT = this.txtEndtime.Text;
                string peple = this.txtLastpep0.Text;
                string cash = this.TxtCashs.Text;
                string aclass = this.Select2.Value;
                if (!string.IsNullOrEmpty(aclass))
                {
                    this.GridView1.DataSource = ac.SearchClss(aclass);
                    this.Select2.Value = "";
                }
                else if (!string.IsNullOrEmpty(sname))
                {
                                  this.AspNetPager1.CurrentPageIndex = 1;
                   this.GridView1.DataSource = ac.SearchName(sname);
                    this.name.Text = "";
                }
                else if (!string.IsNullOrEmpty(peple))
                {
                       this.AspNetPager1.CurrentPageIndex = 1;
                    this.GridView1.DataSource = ac.AaccountLastPep(peple);
                    this.txtLastpep0.Text = "";
                }
                else if (!string.IsNullOrEmpty(startT) && !string.IsNullOrEmpty(endT))
                {
                    this.GridView1.DataSource = ac.TimeAaccount(startT, endT);
                    this.txtstatTime.Text = ""; this.txtEndtime.Text = "";
                }
                else if (!string.IsNullOrEmpty(cash))
                {
                                   this.AspNetPager1.CurrentPageIndex = 1;
                    //this.GridView1.DataSource = ac.AaccountBalane(cash);
                    this.TxtCashs.Text = "";
                }
                 BindGridView();
              
                 this.GridView1.DataBind();
            } /// <summary>
        /// 按照名称查询
        /// </summary>
        /// <param name="sname"></param>
        /// <returns></returns>
        public DataTable SearchName(string sname)
        {
            string SqlString = string.Format("select * from AccountInfo where AccounName like'%{0}%'", sname);
            SqlDataBase MyDb1 = new SqlDataBase();
            return MyDb1.DT(SqlString);
        }    /// <summary>
        /// 按照类别查询
        /// </summary>
        /// <param name="aclass"></param>
        /// <returns></returns>
        public DataTable SearchClss(string aclass)
        {
            string SqlString = string.Format("select * from AccountInfo where class='{0}' order by Accountid desc ", aclass);        SqlDataBase MyDb1 = new SqlDataBase();
            return MyDb1.DT(SqlString);
        }
        /// <summary>
        /// 按照时间段查询
        /// </summary>
        /// <param name="startT"></param>
        /// <param name="endT"></param>
        /// <returns></returns>
        public DataTable TimeAaccount(string startT, string endT)
        { 
            SqlDataBase MyDb1 = new SqlDataBase();
            string sqlTime = string.Format("select * from AccountInfo where AddTime between '{0} 00:00:00' and '{1} 23:59:59' order by Accountid desc ", startT, endT);
          
            return MyDb1.DT(sqlTime);    }
    还是不行啊!
      

  6.   

    [code=csharp][/code]
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using AccountManage.Models;
    using System.Data;
    using Wuqi.Webdiyer;namespace AccountManage
    {
        public partial class Index1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!this.IsPostBack)  //判断网页首次载入
                {                if (Session["UserName"] == "" || Session["UserName"] == null)
                    {
                        Response.Redirect("Login.aspx");                }                if (Convert.ToInt32(Session["Type"].ToString()) != 1)
                    {                    Hidden_Input();                }                BindGridView();            }        }
            //<summary>
            /// 禁用输入框
            /// </summary>
            private void Hidden_Input()
            {
                CheckBox1.Enabled=false;
                txtName.ReadOnly = true;
                txtCash.ReadOnly = true;
                txtAddTime.ReadOnly = true;
                txtLastpep.ReadOnly = true;
                btnOk.Enabled = false;
                btnCancle.Disabled = true;
                Select1.Disabled = true;
            }
            /// <summary>
            /// 添加按钮
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            protected void btnOk_Click(object sender, EventArgs e)
            {            AccountEntity acc = new AccountEntity();
                string Name = this.txtName.Text.Trim();
                double amount = Convert.ToDouble(txtCash.Text.Trim());
                string lastpep = this.txtLastpep.Text.Trim();            DateTime Addtime = DateTime.Now;
                double Cashbalance = 0;
                string Aclass = this.Select1.Value;            UserAction uac = new UserAction();            if (uac.AccountAdd(Aclass, Name, amount, Cashbalance, Addtime, lastpep))
                {
                    ScriptManager.RegisterStartupScript(this, typeof(string), "", "alert('信息录入成功!')", true);                txtName.Text = "";
                    txtCash.Text = "";
                    this.txtLastpep.Text = "";
                    Select1.Value = "";
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, typeof(string), "", "alert('请重新输入');history.go(-1);</script>", true);
                }            BindGridView();
            }
            private void GridBind()
            {
                Account ac = new Account();
                GridView1.DataKeyNames = new string[] { "AccountId" };//主键
                GridView1.DataSource = ac.SearchPaper();
                GridView1.DataBind();
            }
            protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
            {
                this.GridView1.PageIndex = e.NewPageIndex;
                //BindGridView();
            }
            protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundcolor; this.style.backgroundColor='#9caac1';");
                    e.Row.Attributes.Add("onmouseout", "this.style.background=c");            }            if (e.Row.DataItemIndex == -1)
                {
                    return;            }
                if (e.Row.Cells[0].Text == "1")   //判断第三列的值      
                {
                    e.Row.Cells[0].Text = "收入";
                }
                else if (e.Row.Cells[0].Text == "2")
                {
                    e.Row.Cells[0].Text = "支出";
                }
                else
                {
                    e.Row.Cells[0].Text = "修正";
                }
            }
            protected void btnSearch_Click(object sender, ImageClickEventArgs e)
            {
                Account ac = new Account();
                string sname = this.name.Text;
                string startT = this.txtstatTime.Text;
                string endT = this.txtEndtime.Text;
                string peple = this.txtLastpep0.Text;
                string cash = this.TxtCashs.Text;
                string aclass = this.Select2.Value;
                if (!string.IsNullOrEmpty(aclass))
                {
                    //string SqlString = string.Format("select * from AccountInfo where class='{0}' order by Accountid desc ", aclass);
                    //BindGridViewSearch(SqlString);
                    this.GridView1.DataSource = ac.SearchClss(aclass);
                    this.Select2.Value = "";
                }
                else if (!string.IsNullOrEmpty(sname))
                {
                    //string SqlString = string.Format("select * from AccountInfo where AccounName like'%{0}%'", sname);
                    //BindGridViewSearch(SqlString);
                    this.GridView1.DataSource = ac.SearchName(sname);
                    this.name.Text = "";
                }
                else if (!string.IsNullOrEmpty(peple))
                {
                  
                    this.AspNetPager1.CurrentPageIndex = 1;
                    this.GridView1.DataSource = ac.AaccountLastPep(peple);
                    this.txtLastpep0.Text = "";
                }
                else if (!string.IsNullOrEmpty(startT) && !string.IsNullOrEmpty(endT))
                {
                  
                    this.AspNetPager1.CurrentPageIndex = 1;
                    this.GridView1.DataSource = ac.TimeAaccount(startT, endT);
                    this.txtstatTime.Text = ""; this.txtEndtime.Text = "";
                }
                else if (!string.IsNullOrEmpty(cash))
                {
       
                    this.AspNetPager1.CurrentPageIndex = 1;
                    this.GridView1.DataSource = ac.AaccountBalane(cash);
                    this.TxtCashs.Text = "";
                }
                this.AspNetPager1.Visible = true;
                this.GridView1.AllowPaging = true;            this.GridView1.DataBind();
            }
            protected void btnImageUser_Click(object sender, ImageClickEventArgs e)
            {
                Response.Redirect("Admin/UserList.aspx");
            }
            /// <summary>
            /// 显示全部数据
            /// </summary>
            public void BindGridView()
            {
                SqlDataBase mydb = new SqlDataBase();
                string sql = "select * from AccountInfo order by Accountid desc";//自定义的SQL语句
                int recordcount;
                DataSet ds = mydb.GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount);
                this.AspNetPager1.RecordCount = recordcount;
                AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
                AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
                AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
                this.GridView1.DataSource = ds;
                this.GridView1.DataBind();        }        protected void AspNetPager_PageChanged(object sender, EventArgs e)
            {            BindGridView();        }        protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (CheckBox1.Checked == true)
                {
                    btnOk.Enabled = true;
                }
                else
                {
                    btnOk.Enabled = false;
                }        }
            public void BindGridViewSearch(string sql)
            {
                SqlDataBase mydb = new SqlDataBase();
                int recordcount;
                DataSet ds = mydb.GetPage(sql, this.AspNetPager1.CurrentPageIndex, this.AspNetPager1.PageSize, out recordcount);
                this.AspNetPager1.RecordCount = recordcount;
                AspNetPager1.CustomInfoHTML = "记录总数:<b>" + AspNetPager1.RecordCount.ToString() + "</b>";
                AspNetPager1.CustomInfoHTML += " 总页数:<b>" + AspNetPager1.PageCount.ToString() + "</b>";
                AspNetPager1.CustomInfoHTML += " 当前页:<font color=\"red\"><b>" + AspNetPager1.CurrentPageIndex.ToString() + "</b></font>";
                this.GridView1.DataSource = ds;
                this.GridView1.DataBind();          
            }
        }
    }
      

  7.   

    PageChanging,指定gridview索引页,绑定后,重新绑定PageChanged bindData()
      

  8.   

    你把你的where 条件用Session保存。在Sql语句那里写 select * from table where Session["XX"].toString()