我按照3个标准做了一个过滤器表单,因为我没有从GridView的第二页加载数据,所以我在分页上遇到了问题,显然我必须调用我填充的函数加载页面事件中的GridView,但是此函数接收用户在搜索过滤器中输入的参数,我怎样才能在每次页面更改时调用在页面加载事件中将参数发送到该函数的函数在GridView?
功能
public DataTable AdvertSearch(string tittle, DateTime datel, DateTime date2)
{
SqlConnection con = new SqlConnection(Util.GetConnectionString("ConnectionString"));
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "select*from table where titAdvert=@tittle or dateStart=@dateS" or dateEnd=@dateE;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@tittle", tittle== "" ? (object)DBNull.Value : tittle);
cmd.Parameters.AddWithValue("@dateS", date1== "" ? (object)DBNull.Value : date1);
cmd.Parameters.AddWithValue("@dateE", date2== "" ? (object)DBNull.Value : date2);
cmd.Connection = con;
con.Open();
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
搜索按钮
protected void BtnSearch_Click(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}
答案 0 :(得分:1)
您需要指定pageIndexChanging事件,以便在用户单击下一页时触发网格重新加载。看看下面的代码:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindData();
}
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = AdvertSearch(txtTitle.Text,txtDateI.Text,txtDateF.Text);
GridView1.DataBind();
}
请用英语编写代码,因为它使所有程序员都更具可读性。