我有一个文本框,我在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 :(得分:1)
我会使用dataview和rowfilter。您还可以选择将OriginalDataTable缓存在sessionvariable中。
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 :(得分: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+ "%'";
}
也可以在分页上调用此绑定方法。 希望这会有所帮助..
答案 2 :(得分:0)
在会话中保存搜索参数不是更快,检查它是否在里面并再次搜索(因为它似乎没有保存你的文本数据)
答案 3 :(得分: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();
}
答案 4 :(得分: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();
}
多数人