当我更改页码时,我收到错误
"The GridView 'GridView1' fired event PageIndexChanging which wasn't handled."
但之后,我搜索并尝试将此代码放入PageIndexChanging中,但仍无效:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.SelectedIndex = e.NewPageIndex;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}
最初,当用户提示页面时,我在gridview上显示所有数据,然后用户可以搜索数据,点击搜索按钮后,下面的代码正在执行:
protected void Button1_Click(object sender, EventArgs e)
{
DateTime dt1 = DateTime.Now, dt2 = DateTime.Now;
Connection.getCon();
try
{
dt1 = Convert.ToDateTime(TextBox3.Text);
dt2 = Convert.ToDateTime(TextBox4.Text).AddDays(1);
lblError.Visible = false;
}
catch (Exception exc) {
lblError.Visible = true;
}
string cmd = "select * from tblLogs where (users like '%"+TextBox1.Text.Trim()+"%') and (request like '%"+TextBox2.Text.Trim()+"%') and (requesttime>='"+dt1+"') and (requesttime<'"+dt2+"') ";
SqlDataSource1.SelectCommand = cmd;
DataView dv= (DataView) SqlDataSource1.Select(DataSourceSelectArguments.Empty);
GridView1.DataSourceID = null;
GridView1.DataSource= dv;
GridView1.DataBind();
//GridView1.AllowPaging = false;
}
现在,我没有收到任何错误,但页面仍未改变并保持在1。
感谢。
答案 0 :(得分:0)
您在PageIndexChanging事件中使用了错误的属性,它应该是PageIndex而不是SelectedIndex:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = SqlDataSource1;
GridView1.DataBind();
}