如何只显示带有分页的过滤数据..? [英] how to display only filtered data with paging on..?
问题描述
我有一个gridview,文本框和一个搜索按钮。当我搜索我的gridview时,只有第一页显示正确的数据。当我点击下一页时,它会显示所有不需要的数据以及结果。假设我有10条符合我标准的记录,而我的页面大小是5.我只想要2页中的过滤数据..不是所有的数据。怎么做才有帮助。我已经通过点击搜索按钮点击这样做了..但是当我搜索时我想要我的分页。
这是我的搜索按钮代码
protected void SearchButton_Click(object sender,EventArgs e)
{
GridView1.AllowPaging = false ;
GridView1.DataBind();
DataTable dt2 = new DataTable();
//数据库连接字符串//
SqlConnection con = new SqlConnection(ss);
string query = string.Format(从Product.CategoryId上的Product inner join Category中选择ProductId,ProductName,Description,Image,CategoryName,IsActive = Category.CategoryId,其中ProductName如'%'+ SearchTextBox.Text.Replace(%,[%])。替换(','')+%');
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt2 );
GridView1.DataSource = dt2;
GridView1.DataBind();
}
我的分页代码
public void BindTable()
$
SqlConnection con = new SqlConnection(ss);
SqlCommand cmd = new SqlCommand(选择ProductId,ProductName,Description, Product,CategoryName,IsActive from Product inner join Category on Product.CategoryId = Category.CategoryId);
cmd.CommandType = CommandType.Text;
cmd.Connection = con ;
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindTable();
}
aspx代码
i have a gridview, textbox and a search button . when i search my gridview only first page displays the correct data. when i click on next page it shows all the unwanted data also with the results. suppose i have 10 records matching my criteria and my page size is 5. i just want my filtered data in 2 pages ..not all of the data. how to do it plz help . i have done this with paging off on search button click.. but i want my paging on when i search .
here is my search button code
protected void SearchButton_Click(object sender, EventArgs e)
{
GridView1.AllowPaging = false;
GridView1.DataBind();
DataTable dt2 = new DataTable();
//Database connectionstring//
SqlConnection con = new SqlConnection(ss);
string query = string.Format("Select ProductId,ProductName,Description,Image,CategoryName,IsActive from Product inner join Category on Product.CategoryId=Category.CategoryId where ProductName like '%" + SearchTextBox.Text.Replace("%", "[%]").Replace("'", "''") + "%'");
SqlCommand cmd = new SqlCommand(query);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt2);
GridView1.DataSource = dt2;
GridView1.DataBind();
}
my paging code
public void BindTable()
{
SqlConnection con = new SqlConnection(ss);
SqlCommand cmd = new SqlCommand("Select ProductId,ProductName,Description,Image,CategoryName,IsActive from Product inner join Category on Product.CategoryId=Category.CategoryId");
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
SqlDataAdapter ada = new SqlDataAdapter(cmd);
ada.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindTable();
}
aspx code
<asp:GridView ID="GridView1" runat="server" DataKeyNames="ProductId" AutoGenerateColumns="False"
AllowPaging="True" PageSize="5"
OnPageIndexChanging="GridView1_PageIndexChanging" CellPadding="4"
Font-Names="Lucida Sans Unicode" ForeColor="#333333" GridLines="None"
Height="124px" AllowSorting="True" OnSorting="GridView1_Sorting"
Width="814px">
推荐答案
这篇关于如何只显示带有分页的过滤数据..?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!