2013-07-31 55 views
0

我有一個文本框,我寫了一個詞在gridview中搜索。該研究適用於我的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()。

我將不勝感激您的幫助。

謝謝!

+0

研究重置?如果我沒有錯,你希望搜索結果集在分頁過程中保持不變?對 ?如果是這樣,爲操作創建兩個查詢,維護狀態保持在viewstate中的文本值,並根據viewstate值檢查其null或空值填充你的gridview。 –

回答

0

是不是更快只是保存會話的搜索參數,檢查它是否是內並重新搜索它(因爲它似乎文本數據不保存)

+0

那麼,如果我們使用Viewstate來存儲文本數據將會非常好。 –

0

聲明分開方法

在頁面索引chenging
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(); 
} 
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(); 
    } 

這就是所有

1

我會用一個數據視圖的RowFilter和。您也可以選擇將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); 
    } 

} 

在緩存所述數據表成sessionvariable的情況下:

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; 
    } 

} 

問候。

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+ "%'"; 
    } 

也在分頁中調用此綁定方法。 希望這會有所幫助..