2014-01-08 34 views
0

我正在GridView中实现功能显示记录,而不是按默认页码,但按部门号码。例如 我有20个部门,有学生,所以我想显示部门标识符,即D1,D2,D3 ...作为页码,并点击该部门的学生将被加载到网格中。为所需的寻呼机风格的Gridview分页定制页码

我想要像附加图像一样的分页,点击页码中的“...”,它应该带我到下一组页码。 Grid

我应该如何去执行asp.net gridview这样的分页?

回答

0

它将使用“下一步”和“上一步”按钮分页记录。 Label控件将在分页的GridView中显示我们当前的位置。我们首先设置一些变量。

protected int currentPageNumber = 1; 
private const int PAGE_SIZE = 10; 

的currentPageNumber表示在GridView的当前页面,并且是PAGE_SIZE的每一页上显示的记录的总数。您还可以允许用户使用DropDownList调整页面大小,但本文未涉及。

接下来,我们需要将数据源绑定到GridView。让我们来看看整个BindData方法,稍后我会解剖它,以便您有更好的主意。

private void BindData() 
{ 
    string connectionString = "Server=localhost;" + 
      "Database=Northwind;Trusted_Connection=true"; 
    SqlConnection myConnection = new SqlConnection(connectionString); 
    SqlCommand myCommand = new SqlCommand("usp_GetProducts", 
              myConnection); 
    myCommand.CommandType = CommandType.StoredProcedure; 

    myCommand.Parameters.AddWithValue("@startRowIndex", 
             currentPageNumber); 
    myCommand.Parameters.AddWithValue("@maximumRows", PAGE_SIZE); 
    myCommand.Parameters.Add("@totalRows", SqlDbType.Int, 4); 
    myCommand.Parameters["@totalRows"].Direction = 
         ParameterDirection.Output; 

    SqlDataAdapter ad = new SqlDataAdapter(myCommand); 

    DataSet ds = new DataSet(); 
    ad.Fill(ds); 

    gvProducts.DataSource = ds; 
    gvProducts.DataBind(); 

    // get the total rows 
    double totalRows = (int)myCommand.Parameters["@totalRows"].Value; 

    lblTotalPages.Text = CalculateTotalPages(totalRows).ToString(); 

    lblCurrentPage.Text = currentPageNumber.ToString(); 

    if (currentPageNumber == 1) 
    { 
     Btn_Previous.Enabled = false; 

     if (Int32.Parse(lblTotalPages.Text) > 0) 
     { 
      Btn_Next.Enabled = true; 
     } 
     else 
      Btn_Next.Enabled = false; 

    } 

    else 
    { 
     Btn_Previous.Enabled = true; 

     if (currentPageNumber == Int32.Parse(lblTotalPages.Text)) 
      Btn_Next.Enabled = false; 
     else Btn_Next.Enabled = true; 
    } 
} 

现在,让我们来更详细地看看上面的代码。我将currentPageNumber和PAGE_SIZE发送到数据库中,以便可以获取当前页面的数据。 totalRows变量返回表中的总行数。一旦我有totalRows,我计算将用于此GridView的页面总数。的总页数是通过使用一个小的辅助函数计算:

private int CalculateTotalPages(double totalRows) 
{ 
    int totalPages = (int) Math.Ceiling(totalRows/PAGE_SIZE); 

    return totalPages; 
} 

在BindData方法结束时,也有一些条件的检查来确保下一个和上按钮时,才显示适用。

装上活动的按钮

所剩下的最后一件事是将事件附加到Button控件。查看下面的代码,我创建了两个Button控件。

<asp:Button ID="Btn_Previous" CommandName="Previous" 
      runat="server" OnCommand="ChangePage" 
      Text="Previous" /> 
<asp:Button ID="Btn_Next" runat="server" CommandName="Next" 
      OnCommand="ChangePage" Text="Next" /> 

这两个按钮调用这将在下面示出的ChangePage事件:

// This method will handle the navigation/ paging index 
protected void ChangePage(object sender, CommandEventArgs e) 
{ 

    switch (e.CommandName) 
    { 
     case "Previous": 
      currentPageNumber = Int32.Parse(lblCurrentPage.Text) - 1; 
      break; 

     case "Next": 
      currentPageNumber = Int32.Parse(lblCurrentPage.Text) + 1; 
      break; 
    } 

    BindData(); 
} 

的ChangePage事件是用来改变在GridView的页号,并且还通过调用BindData更新标签文本方法。

Source

+0

感谢维涅什,但我没有看到为D1,D2的处理,这里处理.. D12页码,可能是我错过了一些部分,可以请你解释我如何处理,也就是我正面临一个重大问题。 –