从 gridview 中的分页更改页面编号时页面正在刷新

Page is getting refreshed while changing the page no from paging in gridview

我已经在我的 gridview 中实现了 paging 功能。下面是代码

<asp:GridView ID="grdCMPChangeDetails" runat="server" CssClass="table table-bordered" AutoGenerateColumns="false" AllowPaging="true" PageSize="1" PageIndex="1"
                                        OnPageIndexChanging="grdCMPChangeDetails_PageIndexChanging">
                                        <Columns>
                                            <asp:BoundField ItemStyle-Width="150px" DataField="SAP-ID" HeaderText="SAP ID" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="LAT" HeaderText="Latitude" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="LONG" HeaderText="Longitude" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="R4GSTATE" HeaderText="State" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="OLD_LATITUDE" HeaderText="Old Latitude" />
                                            <asp:BoundField ItemStyle-Width="150px" DataField="OLD_LONGITUDE" HeaderText="Old Longitude" />
                                        </Columns>
                                    </asp:GridView>

CS

数据绑定

grdCMPChangeDetails.DataSource = dtSuccessRecord;
                    grdCMPChangeDetails.DataBind();

protected void grdCMPChangeDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdCMPChangeDetails.PageIndex = e.NewPageIndex;
    grdCMPChangeDetails.DataBind();
}

但是在更改页面编号时,它不起作用,而是页面正在刷新。

我认为您需要将网格视图放入更新面板以避免您的问题:

<asp:UpdatePanel ID="myUpdPanel" runat="server" UpdateMode="Conditional">
   <asp:GridView ID="grdCMPChangeDetails" runat="server" CssClass="table table-bordered" 
    AutoGenerateColumns="false" AllowPaging="true" PageSize="1" PageIndex="1"                                       
    OnPageIndexChanging="grdCMPChangeDetails_PageIndexChanging">
   <Columns>
      <asp:BoundField ItemStyle-Width="150px" DataField="SAP-ID" HeaderText="SAP ID" />
      <asp:BoundField ItemStyle-Width="150px" DataField="LAT" HeaderText="Latitude" />
      <asp:BoundField ItemStyle-Width="150px" DataField="LONG" HeaderText="Longitude" />
      <asp:BoundField ItemStyle-Width="150px" DataField="R4GSTATE" HeaderText="State" />
      <asp:BoundField ItemStyle-Width="150px" DataField="OLD_LATITUDE" HeaderText="Old Latitude" />
      <asp:BoundField ItemStyle-Width="150px" DataField="OLD_LONGITUDE" HeaderText="Old Longitude" />
   </Columns>
   </asp:GridView>
</asp:UpdatePanel>

我能注意到的是你调用了网格控件的DataBind方法。您必须通过完整的绑定过程才能填充更改。取而代之的是,将您的 PageIndexChanging 事件更改为:

protected void grdCMPChangeDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdCMPChangeDetails.PageIndex = e.NewPageIndex;
    // Fetch your results from db again or save your search results in ViewState so that you are not hitting the database again and again
    var dtSuccessRecord = SomeDatabaseCall(); 
    // Attach it to the Grid Control again and then head towards binding it.
    grdCMPChangeDetails.DataSource = dtSuccessRecord;
    grdCMPChangeDetails.DataBind();
}

首先将Grid放入Update Panel

<asp:UpdatePanel ID="updatePanel" runat="server">
   <ContentTemplate>
       //YourGrid
   </ContentTemplate>
</asp:UpdatePanel>

你只需要创建一个方法来填充 GridView 像这样

private void populateGrid()
{
   //Code For Populating GridView
}

像这样放在页面加载上

protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {                
                populateGrid();
            }
        }

on Page Index 像这样

   protected void grdCMPChangeDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            try
            {
                grdCMPChangeDetails.PageIndex = e.NewPageIndex;
                populateGrid();              
            }
            catch (Exception ex)
            {

            }
        }

大功告成