0

我有一个文本框,我在其中写一个单词以在 gridview 中搜索。该研究适用于我的 gridview 的第一页,但是当我转到另一页时,研究会重置。

这是我的代码:

using System;
using System.Data;
using System.Configuration;
using System.Text.RegularExpressions;
using System.Web;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Generic;
using System.IO.Compression;
using System.IO;


public partial class _Default : Page
{

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


    private void BindData()
    {
        string query = "select * from Ressources";
        SqlCommand cmd = new SqlCommand(query);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();      
    }


    private DataTable GetData(SqlCommand cmd)
    {
        string strConnString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using (SqlConnection con = new SqlConnection(strConnString))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    return dt;
                }
            }
        }
    }


    protected void EditCustomer(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        this.BindData();
    }


    protected void CancelEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindData();

    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindData();
    }


    private void BindData(string Query)
    {
        string connectionstring = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
        using (SqlConnection conn = new SqlConnection(connectionstring))
        {
            conn.Open();
            using (SqlCommand comm = new SqlCommand(Query + ";select * from Ressources", conn))
            {
                SqlDataAdapter da = new SqlDataAdapter(comm);
                DataSet ds = new DataSet();
                da.Fill(ds);
                GridView1.DataSource = ds;
                GridView1.DataBind();
            }
        }
    }


    protected void RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        ...
    }

    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (GridView1.EditIndex >= 0)
            return;

        if ((e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate) &&
        (e.Row.RowType == DataControlRowType.DataRow || e.Row.RowType == DataControlRowType.Header))
        {
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[14].Visible = false;
            e.Row.Cells[15].Visible = false;
        }
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        ...
    }


    private void AddNewRecord(string URL, string Type_Source, string First_date, string Data, string Crawler_subcategory)
    {
        ...
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        ...
    }

    public void btnSearch_Click(object sender, EventArgs e)
    {
        string query = "select * from Ressources where data like'%" + txtSearch.Text + "%'";
        SqlCommand cmd = new SqlCommand(query);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

}

用于搜索单词的函数名为 btnSearch_Click()。

我会很感激你的帮助。

谢谢 !

4

5 回答 5

1

我会使用数据视图和行过滤器。您还可以选择将 OriginalDataTable 缓存在会话变量中。

public partial class WebForm1 : System.Web.UI.Page
{
    // Hold the original datatable from database
    System.Data.DataTable OriginalDataTable = null; 

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

    void BindGridView(string searchQuery )
    {

        GridView1.DataSource = GetSelectionResult(searchQuery);
        GridView1.DataBind(); 

    }

    private void initialData()
    {
        string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["defaultconnection"].ConnectionString;
        string query = "select * from Ressources";

        OriginalDataTable = new DataTable(); 

        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connectionString))
        {
            dataAdapter.Fill(OriginalDataTable); 
        }

    }


    DataView GetSelectionResult(string searchParam)
    {
        if (OriginalDataTable == null)
            initialData();

        if (string.IsNullOrEmpty(searchParam))
            return OriginalDataTable.DefaultView;

        string rowFilter = string.Format("data like '%{0}%'", searchParam);
        return new DataView(OriginalDataTable, rowFilter, "data", DataViewRowState.OriginalRows); 

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        BindGridView(TextBox1.Text); 
    }

    protected void GridView1_PageIndexChanged(object sender, EventArgs e)
    {
        //...
    }

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGridView(TextBox1.Text); 
    }

}

在将数据表缓存到会话变量中的情况下:

private void initialData()
{
    string connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["defaultconnection"].ConnectionString;
    string query = "select * from Ressources";

    if (Session["datatableinsession"] == null)
    {        
        OriginalDataTable = new DataTable();
        using (SqlDataAdapter dataAdapter = new SqlDataAdapter(query, connectionString))
        {
            dataAdapter.Fill(OriginalDataTable);
        }
    }
    else
    {
        OriginalDataTable = Session["datatableinsession"] as DataTable;
    }

}

问候。

于 2013-07-31T11:02:59.407 回答
1
public void btnSearch_Click(object sender, EventArgs e)
    {

     BindData();

    }
    Private xxx BindData()
    {
     if(Viewstate[txt] !==null)
         {
          string WhereCl= GetWhereClause(txt);
         }
        string query = "select * from Ressources";
         if(!string.IsNullOrEmpty(WhereCl))
       { 
         query =query + WhereCl;
       }         
        SqlCommand cmd = new SqlCommand(query);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

    Private string GetWhereClause(string txt)
    {
      string where  = where data like'%" + txt+ "%'";
    }

在分页上也调用此绑定方法。希望这可以帮助 ..

于 2013-08-05T14:17:53.400 回答
0

将单独的方法声明为

private void Search()
{
string query = "select * from Ressources where data like'%" + txtSearch.Text + "%'";
        SqlCommand cmd = new SqlCommand(query);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
}

调用Search()页面索引成

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        Search();
    }

您可以在单击搜索按钮时调用相同的方法。

public void btnSearch_Click(object sender, EventArgs e)
{
   Search();
}
于 2013-07-31T09:53:50.320 回答
0

仅创建一种方法来绑定数据,如下所示:

private void BindData()
    {
        string query = "";
        if (txtSearch.Text != "" &&  txtSearch.Text != string.Empty) {
            query = "select * from Ressources where data like'%" + txtSearch.Text + "%'";                 
        } else {
             query = "select * from Ressources";
        }

        SqlCommand cmd = new SqlCommand(query);
        GridView1.DataSource = GetData(cmd);
        GridView1.DataBind();
    }

就这样

于 2013-07-31T09:56:06.780 回答
0

在会话中保存搜索参数不是更快吗,检查它是否在里面并再次搜索(因为看起来你的文本数据没有保存)

于 2013-07-31T09:51:29.520 回答