c# webform 搜索结果的分页有问题
c# webform 搜索结果的分页有问题需求: 对搜索结果进行分页显示。
主要控件:
textbox,button,gridview
问题: 点击第2页后, 发现gridview 是按select * from table 的SQL 语句显示的数据, 而不是条件查询的select * from table where X==XX的查询SQL 数据。
代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace WebIMS
{
public partial class Order_Search : System.Web.UI.Page
{
public string source = "Select Order_ID AS ID,Customer AS 客户,Order_Name AS 商品名称,Number AS 数量,Total_Price AS 合计,Date_Delivery AS 交货日期,Create_Name AS 创建人 from Order_Info order by ID desc"; //查询SQL 语句
protected void Page_Load(object sender, EventArgs e)
{
/*
if (!IsPostBack) //如果是首次加载页面
{
bind();
}
*/
}
public void bind()
{
//开始 载入GridView1
//string source = "Select Order_ID AS ID,Customer AS 客户,Order_Name AS 商品名称,Number AS 数量,Total_Price AS 合计,Date_Delivery AS 交货日期,Create_Name AS 创建人 from Order_Info order by ID desc";
SqlConnection conn = new SqlConnection(DBHelper.constr);
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(source, conn);//实例化一个DataAdapter
DataSet objDataSet1 = new DataSet();//实例化一个DataSet
sqlDataAdapter1.Fill(objDataSet1, "temp_table");//把符合条件的数据填充到DataSet.
if (objDataSet1.Tables[0].Rows.Count != 0) //判断DataSet是否有返回记录
{
this.GridView1.DataSource = objDataSet1.Tables["temp_table"];
this.GridView1.DataBind();
}
else
{
Notice.Text = "没有记录!";
this.GridView1.DataSource = null;
this.GridView1.DataBind();
}
// 载入GridView1结束
}
void DisplayCurrentPage()
{
// Calculate the current page number.
int currentPage = this.GridView1.PageIndex + 1;
// Display the current page number.
//Current_Page.Text = "Page " + currentPage.ToString() + " of " +this.GridView1.PageCount.ToString() + ".";
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Call a helper method to display the current page number
// when the page is first loaded.
DisplayCurrentPage();
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Notice.Text = "";
SqlConnection connection = new SqlConnection(DBHelper.constr);
SqlCommand cmd01 = connection.CreateCommand();
connection.Open();
= "Select Order_ID AS ID,Customer AS 客户,Order_Name AS 商品名称,Number AS 数量,Total_Price AS 合计,Date_Delivery AS 交货日期,Create_Name AS 创建人,Create_Time AS 创建时间,Update_Name AS 更新人,Update_Time AS 更新时间,Adds_Delivery AS 交货地点,Unit_Price AS 单价,Notes AS 备注 from Order_Info Where Order_ID ='" + this.GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text + "'";
SqlDataReader reader = cmd01.ExecuteReader();
while (reader.Read())
{
Order_ID.Text = reader.IsDBNull(0) ? "" : reader.GetInt32(0).ToString().Trim();
Customer.Text = reader.IsDBNull(1) ? "" : reader.GetString(1).ToString().Trim();
Order_Name.Text = reader.IsDBNull(2) ? "" : reader.GetString(2).ToString().Trim();
Number.Text = reader.IsDBNull(3) ? "" : reader.GetDecimal(3).ToString().Trim();
Total_Price.Text = reader.IsDBNull(4) ? "" : reader.GetDecimal(4).ToString().Trim();
Date_Delivery.Text = reader.IsDBNull(5) ? "" : reader.GetDateTime(5).ToString().Trim();
Create_Name.Text = reader.IsDBNull(6) ? "" : reader.GetString(6).ToString().Trim();
Create_Time.Text = reader.IsDBNull(7) ? "" : reader.GetDateTime(7).ToString().Trim();
Update_Name.Text = reader.IsDBNull(8) ? "" : reader.GetString(8).ToString().Trim();
Update_Time.Text = reader.IsDBNull(9) ? "" : reader.GetDateTime(9).ToString().Trim();
Adds_Delivery.Text = reader.IsDBNull(10) ? "" : reader.GetString(10).ToString().Trim();
Unit_Price.Text = reader.IsDBNull(11) ? "" : reader.GetDecimal(11).ToString().Trim();
Notes.Text = reader.IsDBNull(12) ? "" : reader.GetString(12).ToString().Trim();
}
connection.Close();
Notice.Text = "当前选中的记录ID是:" + this.GridView1.Rows[GridView1.SelectedIndex].Cells[1].Text;
}
protected void GridView1_PageIndexChanged(object sender, EventArgs e)
{
DisplayCurrentPage();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
}
protected void Button_Search_Click(object sender, EventArgs e)
{
Notice2.Text = "";
string Search_String1 = ""; //存储查询字段
string Search_String2 = "";
switch (Search_Drop1.SelectedItem.ToString())
{
case "客户":
Search_String1 = "Customer";
break;
case "商品名称":
Search_String1 = "Order_Name";
break;
case "数量":
Search_String1 = "Number";
break;
case "单价":
Search_String1 = "Unit_Price";
break;
case "备注":
Search_String1 = "Notes";
break;
case "交货方式":
Search_String1 = "Delivery";
break;
case "交货地点":
Search_String1 = "Adds_Delivery";
break;
case "交货日期":
Search_String1 = "Date_Delivery";
break;
case "合计":
Search_String1 = "Total_Price";
break;
case "创建人":
Search_String1 = "Create_Name";
break;
case "更新人":
Search_String1 = "Update_Name";
break;
default:
Search_String1 = "Customer";
break;
}
switch (Search_Drop2.SelectedItem.ToString())
{
case "客户":
Search_String2 = "Customer";
break;
case "商品名称":
Search_String2 = "Order_Name";
break;
case "数量":
Search_String2 = "Number";
break;
case "单价":
Search_String2 = "Unit_Price";
break;
case "备注":
Search_String2 = "Notes";
break;
case "交货方式":
Search_String2 = "Delivery";
break;
case "交货地点":
Search_String2 = "Adds_Delivery";
break;
case "交货日期":
Search_String2 = "Date_Delivery";
break;
case "合计":
Search_String2 = "Total_Price";
break;
case "创建人":
Search_String2 = "Create_Name";
break;
case "更新人":
Search_String2 = "Update_Name";
break;
default:
Search_String2 = "Customer";
break;
}
if ((Search_Key1.Text.Trim().Length > 0) && (Search_Key2.Text.Trim().Length == 0)) //输入第一个关键字
{
source = "Select Order_ID AS ID,Customer AS 客户,Order_Name AS 商品名称,Number AS 数量,Total_Price AS 合计,Date_Delivery AS 交货日期,Create_Name AS 创建人 from Order_Info where " + Search_String1 + " like '%" + Search_Key1.Text.Trim() + "%'order by ID desc";
}
else if ((Search_Key1.Text.Trim().Length > 0) && (Search_Key2.Text.Trim().Length > 0)) //输入前两个关键字
{
source = "Select Order_ID AS ID,Customer AS 客户,Order_Name AS 商品名称,Number AS 数量,Total_Price AS 合计,Date_Delivery AS 交货日期,Create_Name AS 创建人 from Order_Info where " + Search_String1 + " like '%" + Search_Key1.Text.Trim() + "%'and " + Search_String2 + " like '%" + Search_Key2.Text.Trim() + "%'order by ID desc";
}
else
{
source = "Select Order_ID AS ID,Customer AS 客户,Order_Name AS 商品名称,Number AS 数量,Total_Price AS 合计,Date_Delivery AS 交货日期,Create_Name AS 创建人 from Order_Info order by ID desc";
}
Notice.Text = "Search_String1:" + Search_String1 + "like" + Search_Key1.Text.Trim() + " Search_String2:" + Search_String2 + "like" + Search_Key2.Text.Trim();
SqlConnection conn = new SqlConnection(DBHelper.constr);
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(source, conn);//实例化一个DataAdapter
DataSet objDataSet1 = new DataSet();//实例化一个DataSet
sqlDataAdapter1.Fill(objDataSet1, "temp_table");//把符合条件的数据填充到DataSet.
if (objDataSet1.Tables[0].Rows.Count != 0) //判断DataSet是否有返回记录
{
// MessageBox.Show("有数据");
this.GridView1.DataSource = objDataSet1.Tables["temp_table"];
this.GridView1.DataBind();
}
else
{
Notice2.Text = "没有满足条件的记录!";
this.GridView1.DataSource = null;
this.GridView1.DataBind();
}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex; //设置当前页索引
//开始 载入GridView1
SqlConnection conn = new SqlConnection(DBHelper.constr);
SqlDataAdapter sqlDataAdapter1 = new SqlDataAdapter(source, conn);//实例化一个DataAdapter
DataSet objDataSet1 = new DataSet();//实例化一个DataSet
sqlDataAdapter1.Fill(objDataSet1, "temp_table");//把符合条件的数据填充到DataSet.
if (objDataSet1.Tables[0].Rows.Count != 0) //判断DataSet是否有返回记录
{
this.GridView1.DataSource = objDataSet1.Tables["temp_table"];
this.GridView1.DataBind();
}
else
{
Notice.Text = "没有相关记录!";
this.GridView1.DataSource = null;
this.GridView1.DataBind();
}
// 载入GridView1结束
}
}
}