【问题标题】:Unable to use paging despite enabling paging尽管启用了分页,但无法使用分页
【发布时间】:2013-07-19 06:54:36
【问题描述】:

我正在尝试允许对我的 gridview 进行分页。我在我的 gridview 中有允许分页,还添加了页面大小。不幸的是,它不起作用。我进行了研究,发现有人不需要添加任何代码。

这是我的 gridview 的源代码

<asp:GridView ID="GVVerify" runat="server" AllowPaging="True" AutoGenerateSelectButton="True" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" CssClass="gridviewadminverify" ForeColor="Black" OnSelectedIndexChanged="GVVerify_SelectedIndexChanged" Width="100%">
                <FooterStyle BackColor="#CCCCCC" />
                <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
                <RowStyle BackColor="White" />
                <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#F1F1F1" />
                <SortedAscendingHeaderStyle BackColor="#808080" />
                <SortedDescendingCellStyle BackColor="#CAC9C9" />
                <SortedDescendingHeaderStyle BackColor="#383838" />
            </asp:GridView>

这就是我通过数据绑定连接我的 SQL 的方式,这与使用数据源不同。

SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI";
conn.Open();

DataSet ds = new DataSet();

SqlDataAdapter da = new SqlDataAdapter("SELECT policeid as [Police ID], password as [Password], email as [Email], nric as [NRIC], fullname as [Full Name], contact as [Contact], address as [Address], location as [Location] From LoginRegisterPolice where pending='pending'", conn);
da.Fill(ds);

GVVerify.DataSource = ds;           
GVVerify.DataBind();
conn.Close();

【问题讨论】:

    标签: c# asp.net gridview paging


    【解决方案1】:

    您没有使用 PageIndexChanging 事件,您必须绑定 PageIndexChanging 事件并使用当前页面重新绑定您的网格。

    HTML

    <asp:GridView ID="GVVerify" runat="server"  OnPageIndexChanging="GridViewPageEventHandler" AllowPaging="True" AutoGenerateSelectButton="True" BackColor="#CCCCCC" 
    BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" 
    CssClass="gridviewadminverify" ForeColor="Black" OnSelectedIndexChanged="GVVerify_SelectedIndexChanged" 
    Width="100%">
    

    背后的代码

    protected void grdView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GVVerify.PageIndex = e.NewPageIndex;
        GVVerify.DataSource = GetGridData();
        GVVerify.DataBind();
    }
    

    【讨论】:

      【解决方案2】:

      你需要处理gridview的PageIndexChanging事件。

      例如:

      protected void GVVerify_PageIndexChanging(object sender, GridViewPageEventArgs e)
          {
              bindGrid();
              GVVerify.PageIndex = e.NewPageIndex;
              GVVerify.DataBind();
          }
      

      其中 bindGrid 方法将包含用于绑定网格的代码。 例如

      private void bindGrid()
          {
              SqlConnection conn = new SqlConnection();
              conn.ConnectionString = "Data Source = localhost; Initial Catalog = project; Integrated Security= SSPI";
              conn.Open();
              DataSet ds = new DataSet();
      
              SqlDataAdapter da = new SqlDataAdapter("SELECT policeid as [Police ID], password as [Password], email as [Email], nric as [NRIC], fullname as [Full Name], contact as [Contact], address as [Address], location as [Location] From LoginRegisterPolice where pending='pending'", conn);
              da.Fill(ds);
      
             GVVerify.DataSource = ds;           
             GVVerify.DataBind();
             conn.Close();
          }
      

      【讨论】:

        猜你喜欢
        • 2019-12-25
        • 2017-03-28
        • 2013-08-25
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 2020-08-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多