查询结果用gridview分页,点击下一页报错,求大神帮忙,不要百度糊弄我 15
if(DropDownList1.SelectedIndex==1){stringstrSQL="SelectSid,Rid,Bid,Bname,Borrowdate,r...
if (DropDownList1.SelectedIndex == 1)
{
string strSQL = "Select Sid,Rid,Bid,Bname,Borrowdate,returndate from State where Bname like '%" + TextBox1.Text + "%'";
OracleCommand myComm = new OracleCommand(strSQL, conn);
conn.Open();
OracleDataAdapter adapter = new OracleDataAdapter(myComm);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
this.GridView1.DataSourceID = null;
GridView1.DataSource = dataset.Tables[0];
GridView1.DataBind();
conn.Close();
}
比如我对gridview的pagesize设置为5,我用判断来对数据进行查询,当查到数据大于5个时,我点击第二页会报错,错误提示:GridView“GridView1”激发了未处理的事件“PageIndexChanging”。 我该怎么解决,求大神帮忙 展开
{
string strSQL = "Select Sid,Rid,Bid,Bname,Borrowdate,returndate from State where Bname like '%" + TextBox1.Text + "%'";
OracleCommand myComm = new OracleCommand(strSQL, conn);
conn.Open();
OracleDataAdapter adapter = new OracleDataAdapter(myComm);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
this.GridView1.DataSourceID = null;
GridView1.DataSource = dataset.Tables[0];
GridView1.DataBind();
conn.Close();
}
比如我对gridview的pagesize设置为5,我用判断来对数据进行查询,当查到数据大于5个时,我点击第二页会报错,错误提示:GridView“GridView1”激发了未处理的事件“PageIndexChanging”。 我该怎么解决,求大神帮忙 展开
2个回答
展开全部
你需要做如下改进才能达到设置了查询条件,查询出结果后还能进行下一页的切换:
1. 在GridView 的属性里增加分页响应事件: OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True"
如:
<asp:GridView ID="GridView1" Width="1400" runat="server" AutoGenerateColumns="False" GridLines="None" PageSize="5" EmptyDataText="No Data" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True">
2. 调整你的代码,你现在的代码是不支持分页的,需要进行调整。
最简单办法是:在后台增加缓存数据的变量将从数据库中查询到的数据保存到Session中,然后在进行数据绑定时调用此Session缓存的数据
简单的实例:
/// <summary>
/// 缓存数据集
/// </summary>
protected DataSet CacheDataSet
{
get
{
if (null == Session["DataSet"])
{
Session["DataSet"] = new DataSet();
}
return (DataSet)Session["DataSet"];
}
set
{
Session["DataSet"] = value;
}
}
/// <summary>
/// 获取数据库数据
/// </summary>
private void getDate(string searchCondition)
{
string strSQL = "Select Sid,Rid,Bid,Bname,Borrowdate,returndate from State where Bname like '%" + searchCondition + "%'";
OracleCommand myComm = new OracleCommand(strSQL, conn);
conn.Open();
OracleDataAdapter adapter = new OracleDataAdapter(myComm);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
CacheDataSet = dataset;
conn.Close();
}
/// <summary>
/// 表格数据分页显示
/// </summary>
private void doSearch(int pageIndex)
{
try
{
GridView1.PageIndex = pageIndex;
GridView1.DataSource = CacheDataSet;
GridView1.DataBind();
}
catch (Exception)
{
GridView1.EmptyDataText = "Database ERROR";
GridView1.DataSource = null;
GridView1.DataBind();
GridView1.EmptyDataText = "No Data";
}
}
/// <summary>
/// 处理分页事件
/// </summary>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
doSearch(e.NewPageIndex);
}
// 进行调整后的原来的代码
if (DropDownList1.SelectedIndex == 1)
{
getDate(TextBox1.Text);
doSearch(0);
}
1. 在GridView 的属性里增加分页响应事件: OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True"
如:
<asp:GridView ID="GridView1" Width="1400" runat="server" AutoGenerateColumns="False" GridLines="None" PageSize="5" EmptyDataText="No Data" OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="True">
2. 调整你的代码,你现在的代码是不支持分页的,需要进行调整。
最简单办法是:在后台增加缓存数据的变量将从数据库中查询到的数据保存到Session中,然后在进行数据绑定时调用此Session缓存的数据
简单的实例:
/// <summary>
/// 缓存数据集
/// </summary>
protected DataSet CacheDataSet
{
get
{
if (null == Session["DataSet"])
{
Session["DataSet"] = new DataSet();
}
return (DataSet)Session["DataSet"];
}
set
{
Session["DataSet"] = value;
}
}
/// <summary>
/// 获取数据库数据
/// </summary>
private void getDate(string searchCondition)
{
string strSQL = "Select Sid,Rid,Bid,Bname,Borrowdate,returndate from State where Bname like '%" + searchCondition + "%'";
OracleCommand myComm = new OracleCommand(strSQL, conn);
conn.Open();
OracleDataAdapter adapter = new OracleDataAdapter(myComm);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
CacheDataSet = dataset;
conn.Close();
}
/// <summary>
/// 表格数据分页显示
/// </summary>
private void doSearch(int pageIndex)
{
try
{
GridView1.PageIndex = pageIndex;
GridView1.DataSource = CacheDataSet;
GridView1.DataBind();
}
catch (Exception)
{
GridView1.EmptyDataText = "Database ERROR";
GridView1.DataSource = null;
GridView1.DataBind();
GridView1.EmptyDataText = "No Data";
}
}
/// <summary>
/// 处理分页事件
/// </summary>
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
doSearch(e.NewPageIndex);
}
// 进行调整后的原来的代码
if (DropDownList1.SelectedIndex == 1)
{
getDate(TextBox1.Text);
doSearch(0);
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询