ObjectDataSource and Sorting (Part 2)
Sergio asked if I could post how I solved my problem with sorting and the ObjectDataSource control.
Here’s what I did:
First of all, add a parameter to the method that is being called by the ObjectDataSource control that will be used to sort the data.
<DataObjectMethod(DataObjectMethodType.Select)> \_ Public Shared Function GetTests(ByVal sortExpression As String) As TestInfoCollection If sortExpression Is Nothing Then Throw New ArgumentNullException("sortExpression") End If
It’s up to you what you do with the value that is passed in. The value comes in looking like the sort bit of SQL. In my case to get things up and running I’m loading my data into a DataTable, then use a DataView to do the sorting for me - not the most efficient way but it works for now. Later on, I might pass the sort criteria down to SQL Server and let it do the sorting for me, but that will require messing with the stored procedures!
It seems like an oversight that there isn’t a special attribute to prepend the sortExpression parameter so that the ObjectDataSource recognises it as special - unfortunately it just thinks it’s a regular parameter and will add it to the SelectParameters part in the aspx file. You just need to remove it from there and instead make sure the SortParameterName attribute is set instead.
<asp:ObjectDataSource id="odsTests" runat="server" TypeName="UniSA.UniSAnet.SmartMark.DataAccess.TestComponent" SelectMethod="GetTestsForCourse" SortParameterName="sortExpression"> <SelectParameters> <asp:QueryStringParameter ConvertEmptyStringToNull="False" Name="courseId" QueryStringField="CourseId" Type="Object" /> </SelectParameters> </asp:ObjectDataSource>