使用objectdatasource发布排序gridview

时间:2015-04-07 00:10:10

标签: asp.net sorting gridview vb.net-2010 objectdatasource

我已经阅读了一些有关如何在使用objectdatasource时执行排序和分页的教程。我已经创建了一个动态的sql存储过程来处理分页和排序,一切都在数据库端正常工作。当我点击gridview标题执行排序时,我遇到了问题。我收到以下错误消息:GridView'EmployeeGrid'触发事件排序未处理。我没有在后面的代码中编码的排序事件,我的印象是可以通过将排序表达式从对象数据源传递到我的存储过程来自动处理排序。如果我错了,请纠正我。有人可以提供有关如何使用objectdatasource执行排序的任何建议吗?如果我的配置不正确,请告诉我。此外,分页工作正常。

   <asp:GridView ID="EmployeeGrid" runat="server" Width="100%" AutoGenerateColumns="False"
                ClientIDMode="Static"  EmptyDataText=""   AllowSorting="True" AllowPaging="true" 
                >
                <Columns>                   
                    <asp:BoundField DataField="Employee_ID" HeaderText="Employee ID" SortExpression="Employee_ID" />
                    <asp:BoundField DataField="First_Name" HeaderText="First Name" SortExpression="First_Name" />
                    <asp:BoundField DataField="Last_Name" HeaderText="Last Name" SortExpression="Last_Name" />            
                </Columns>
            </asp:GridView>

  <asp:ObjectDataSource ID="GridDataSource" runat="server" OldValuesParameterFormatString="original_{0}"
    SelectMethod="GetDataByValue" TypeName="PO.BLL.Employee"
    EnablePaging="True" SelectCountMethod="GetDataByValueCount" MaximumRowsParameterName="MaximumRows"
    StartRowIndexParameterName="StartIndex" SortParameterName="sortExpression">
    <SelectParameters>
        <asp:ControlParameter ControlID="txtEmployeeID" Name="Employee_ID" PropertyName="Text"
            Type="String" />
        <asp:ControlParameter ControlID="txtFirstName" Name="First_Name" PropertyName="Text"
            Type="String" />
        <asp:ControlParameter ControlID="txtLastName" Name="Last_Name" PropertyName="Text"
            Type="String" />

    </SelectParameters>
</asp:ObjectDataSource>

   <DataObjectMethod(DataObjectMethodType.[Select])> _
    Public Function GetDataByValue(ByVal Employee_ID As String,
                                       ByVal First_Name As String, _
                                       ByVal Last_Name As String, _
                                       ByVal StartIndex As Integer, _
                                       ByVal MaximumRows As Integer, _
                                       ByVal SortExpression As String
                                       ) As EmployeeDS.EmployeeDataTable



        If String.IsNullOrEmpty(SortExpression) Then
            SortExpression = "Contract_ID"
        End If

        Return Me.Adapter.GetDataByValue(Employee_ID, _
                                                 First_Name, _
                                                 Last_Name, _
                                                 StartIndex, _
                                                 MaximumRows, _
                                                 SortExpression
                                                )

    End Function

   'on search button click
    Private Sub BindData()
      Me.EmployeeGrid.DataSource = GridDataSource
      Me.EmployeeGrid.DataBind()

    End Sub

1 个答案:

答案 0 :(得分:0)

GridView有一个名为“OnSorting”的属性,它是处理事件的后端函数的名称。尝试添加它,看看会发生什么。