asp.net - 分页GridView

标签 asp.net vb.net gridview pagination

我有一个简单的查询:

Dim info As New SailMembersDataContext
Dim query = From p In info.Individuals
GridView1.DataSource = query
GridView1.DataBind()

我想知道如何向此查询添加分页,例如页面上的 10 我曾尝试在 GridView 上使用内置分页,但这只会产生错误:

GridView“GridView1”触发了未处理的事件 PageIndexChanging

尝试切换到另一个页面时。

最佳答案

好吧,这很简单

在后面的代码中处理 PageIndexChanging 事件,

C#

void GridView1_PageIndexChanging(Object sender, GridViewPageEventArgs e)
{
    //For example
    //Cancel the paging operation if the user attempts to navigate
    //to another page while the GridView control is in edit mode. 
    if (GridView1.EditIndex != -1)
    {
        // Use the Cancel property to cancel the paging operation.
        e.Cancel = true;

        // Display an error message.
        int newPageNumber = e.NewPageIndex + 1;
        Message.Text = "Please update the record before moving to page " +
        newPageNumber.ToString() + ".";
    }
    else
    {
        // Clear the error message.
        Message.Text = "";
    }
}

VB.NET

Private Sub GridView1_PageIndexChanging(sender As [Object], e As GridViewPageEventArgs)
    'For example
    'Cancel the paging operation if the user attempts to navigate
    'to another page while the GridView control is in edit mode. 
    If GridView1.EditIndex <> -1 Then
        ' Use the Cancel property to cancel the paging operation.
        e.Cancel = True

        ' Display an error message.
        Dim newPageNumber As Integer = e.NewPageIndex + 1
        Message.Text = "Please update the record before moving to page " & newPageNumber.ToString() & "."
    Else
        ' Clear the error message.
        Message.Text = ""
    End If
End Sub

你的标记应该是这样的:

<asp:gridview id="GridView1" 
    autogeneratecolumns="true"
    emptydatatext="No data available." 
    allowpaging="true"
    autogenerateeditbutton="true"
    onpageindexchanging="GridView1_PageIndexChanging" 
    runat="server">
    <pagersettings mode="Numeric"
      position="Bottom"           
      pagebuttoncount="10"/>

    <pagerstyle backcolor="LightBlue"/>

  </asp:gridview>

关于asp.net - 分页GridView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4862809/

相关文章:

c# - 从 asp.net 页面运行 cmd 或 exe 文件

c# - mysql问题。异常(exception)

vb.net - 从数据表中获取值(value)

c# - 异常 "Cannot access a disposed object"来自非用户代码

html - Gridview 最大宽度(动态列大小)

asp.net - 布局 View 中的 Roles.GetRolesForUser() 不返回角色

c# - 如何从 List<string> 中删除相同的项目?

vb.net - 在 VB.NET 的新线程上引发事件

android - GridView OnItemClickListener - 如何对其他项目进行灰度化

所有列的 WPF GridView 共享单元格模板