我有一个面板ViewStock,我在数据库中查看gridview,通过代码查看DataBind()。允许分页和创建和事件" OnPageIndexChanging"在html的gridview标签中,实现上面定义的代码并在事件中进行分页,如下所示:
HTML:
<asp:Panel ID="Panel_StockView" runat="server">
<asp:GridView ID="GridView_Stock" AllowPaging="true" OnPageIndexChanging="GridView_PageIndexChanging" runat="server"></asp:GridView>
</asp:Panel>
代码C#:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
using(SqlConnection con = new SqlConnection(cs))
{
//Sql command here
/sql adapter and filled datatable
sdaStockView.Fill(dtStockView);
GridView_Stock.DataSource = dtStockView;
GridView_Stock.DataBind();
}
}
现在已实施的分页
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.DataBind();
GridView_Stock.PageIndex = e.NewPageIndex;
}
它确实有效但部分有效。它执行分页并正确地执行数据。但问题是,当我点击页面&#39; 2&#39;面板就像我上传的See this Image图片一样空白,然后我点击链接按钮,再次将我重定向到面板并打开页面&#39; 2&#39;有效数据的gridview。
如何解决此问题?
答案 0 :(得分:2)
将GridView绑定到数据的逻辑提取到新方法中。例如,您可以将其称为BindData()
:
private void BindData()
{
using (SqlConnection con = new SqlConnection(cs))
{
sdastockview.fill(dtstockview);
gridview_stock.datasource = dtstockview;
gridview_stock.databind();
}
}
在LinkButton_Panel_ViewStock_Click
中调用此方法以填充GridView
:
protected void LinkButton_Panel_ViewStock_Click(object sender, EventArgs e)
{
this.BindData();
}
最后,再次调用它以在分页期间重新填充GridView
:
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView_Stock.PageIndex = e.NewPageIndex;
this.BindData();
}
只需进行这三项小修改即可。我已经尝试过这个并且工作得很好。
答案 1 :(得分:0)
在填写DataSet
之后ViewState
将您的LinkButton_Panel_ViewStock_Click
保存在DataSet
的某个地方
ViewState["ds"] = dtStockView
在PageIndexChanging
写这样的
protected void GridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
panel_ViewStock.visible = true;
GridView_Stock.PageIndex = e.NewPageIndex;
GridView_Stock.DataSource = ViewState["ds"] as DataSet
GridView_Stock.DataBind();
}
希望这会对你有所帮助
答案 2 :(得分:0)
您可以尝试使用:
protected void GridView_RowCommand(object sender, GridViewCommandEventArgs e)