【问题标题】:Gridview Sorting event unhandledGridview 排序事件未处理
【发布时间】:2013-08-26 19:36:15
【问题描述】:

我有一个asp gridview,它通过LINQ 连接到我的sql 数据库。我将它绑定在后面的代码中。我也照常做,

 AllowSorting="True"

我为每一列设置排序表达式:ex-

                <asp:BoundField DataField="BorrowerDateOfBirth" HeaderText="Date Of Birth" 
                    DataFormatString="{0:d}" SortExpression="BorrowerDateOfBirth" >
                </asp:BoundField>

但是当我运行应用程序并单击列标题进行排序时,应用程序会触发异常错误,内容如下:

“GridView 'gridview1' 触发了未处理的排序事件。”

我在网上查找了这个错误,但我只找到了与 C# 代码相关的响应。我尝试将它们转换为 vb.net,但错误仍然存​​在。

有谁知道如何在 vb.net 中处理 asp gridview 的排序?

【问题讨论】:

    标签: asp.net vb.net sorting gridview aspxgridview


    【解决方案1】:

    您需要将OnSorting="" 属性设置为某个函数名,然后在该函数中处理排序。类似的东西

    Protected Sub TaskGridView_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs)  
        'Retrieve the table from the session object.
        Dim dt = TryCast(Session("table"), DataTable)
        If dt IsNot Nothing Then 
          'Sorting the data.
          dt.DefaultView.Sort = e.SortExpression & " " &  GetSortingDirection(e.SortExpression)
          TaskGridView.DataSource = Session("TaskTable")
          TaskGridView.DataBind()
        End If
    End Sub
    
    Private Function GetSortingDirection(ByVal column As String) As String
        ' By default, set the sort direction to ascending.
        Dim sortDirection = "ASC"
        ' Retrieve the last column that was sorted.
        Dim sortExpression = TryCast(ViewState("SortExpression"), String)
        If sortExpression IsNot Nothing Then
          ' Check if the same column is being sorted.
          ' Otherwise, the default value can be returned.
          If sortExpression = column Then
            Dim lastDirection = TryCast(ViewState("SortDirection"), String)
            If lastDirection IsNot Nothing _
              AndAlso lastDirection = "ASC" Then
              sortDirection = "DESC"
            End If
          End If
        End If
        ' Save new values in ViewState.
        ViewState("SortDirection") = sortDirection
        ViewState("SortExpression") = column
        Return sortDirection
    End Function
    

    【讨论】:

    • 在这种情况下, OnSorting="" 在您的示例中的函数名称是什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多