用文本框分页筛选器值时出现问题

时间:2019-05-22 10:34:21

标签: c# asp.net

当我通过文本框过滤某些值时,第一页的显示效果很好,但是当我切换到第二页时,它会刷新并再次给我所有值... 代码隐藏:

 protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["Nome"] == null)
            {

                Response.Redirect("Login.aspx");
            }
            if (!Page.IsPostBack)
            {
                bindgrid();
            } 


        }

        private void bindgrid()
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr1"].ToString();
            con.Open();

            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Select * from [Movimentos]";
            cmd.Connection = con;

            DataSet ds = new DataSet();
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            Sqldata.DataSource = ds;
            Sqldata.DataBind();


        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            void bindgrid()
            {
                SqlConnection con = new SqlConnection();
                con.ConnectionString = ConfigurationManager.ConnectionStrings["ConStr1"].ToString();
                con.Open();

                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "Select * from [Movimentos]";
                cmd.Connection = con;

                DataSet ds = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
                Sqldata.DataSource = ds;


            }
        }
        protected void Sqldata_PreRender(object sender, EventArgs e)
        {
            Label1.Text = "Mostrando a página " + (Sqldata.PageIndex + 1).ToString() + " de " + Sqldata.PageCount.ToString();

        }
        protected void Sqldata_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {



                Sqldata.PageIndex = e.NewPageIndex;
                Sqldata.DataSource = (SqlDataSource1);
                SqlDataSource1.DataBind();


        }

        protected void ButnPesquisar_Click(object sender, EventArgs e)
        {
            string filter = "";
            string command = "SELECT * FROM Movimentos WHERE";


            if (textDataMovimento.Text != "")
            {
                filter = filter + "  [Data Movimento] LIKE '%" + textDataMovimento.Text + "%' AND";
            }
            if (TextDataValor.Text != "")
            {
                filter = filter + " [Data Valor] LIKE '%" + TextDataValor.Text + "%' AND";
            }
            if (TextDescricao.Text != "")
            {
                filter = filter + " [Descrição] LIKE '%" + TextDescricao.Text + "%' AND";
            }
            if (TextValor.Text != "")
            {
                filter = filter + " [Valor] LIKE '%" + TextValor.Text + "%' AND";
            }
            if (textTipodeMovimento.Text != "")
            {
                filter = filter + " [Tipo de Movimento] LIKE '%" + textTipodeMovimento.Text + "%' AND";
            }
            if (filter.Length > 0)
            {
                Sqldata.DataSource = SqlDataSource1;
                string FinalFilter = filter.Remove(filter.Length - 3);
                SqlDataSource1.SelectCommand = command + FinalFilter;
                Sqldata.DataBind();
            }
            else
            {
                Sqldata.DataBind();
            }



        }

有解决方案吗?我找不到任何答案,因为似乎没有人使用多文本框进行过滤...我认为那与databind有关吗?我试图将零钱移到另一个地方,但是没有用

0 个答案:

没有答案