在asp.net2.0中ObjectDataSource可以说是业务层和UI层的桥樑。分页方式有有两种。
第二种方法:
一,首先通过智能标记设置数据源
图1.选择要取得的数据的方法所在的对象,framework.business. M03QuestionManageAction
图2.选择返回dataset的方法
自定义分页和排序,即根据当前所在页和页记录大小和排序。而不是一次性取出所有数据。
首先介绍ObjectDataSource关于自定义分页和排序的几个属性.
AllowPaging属性:当要实现自定义分页时必须把AllowPaging值设置为true。
SelectMethod属性:指向获取数据的方法名称,本例设置为GetProducts。其所指向的方法的参数名与ObjectDataSource 的StartRowIndexParameterName、MaximumRowsParameterName、SortParameterName相对应(注意:参数不分先后,但参数名要一一对应相同,否则出错)
StartRowIndexParameterName属性:当前页的第一条数据在查询数据中的位置,它的值与SelectMethod所指向的方法的相应参数,默认值为startRowIndex。
MaximumRowsParameterName属性:当前页面的记录行数,它的值与SelectMethod所指向的方法的相应参数,默认值为maximumRows。
SortParameterName属性:排序表达式,与SelectMethod所指向的方法的排序参数相对应。如果不设置排序,此属性可以为空。在本例中设置为sortExpression。
SelectCountMethod属性:指向查询到的数据总数的方法名称。本例中是GetQuestionsCount。注意:这个是自定义分页必须设置的属性。
framework.business. M03QuestionManageAction
Public Shared Function GetQuestion(ByVal startRowIndex As Integer, ByVal maxinumRows As Integer) As System.Collections.Generic.List(Of framework.dbtables.TBL_QUESTIONZ)
Dim objM03QuestionManageAction As New M03QuestionManageAction()
Dim ds As DataSet = objM03QuestionManageAction.GetByDataSet("select questionclassid,questiontype,questioncontent,qanalyse from tbl_question ")
Dim results As New System.Collections.Generic.List(Of TBL_QUESTIONZ)
Dim currentIndex As Integer = startRowIndex
Dim itemRead As Integer = 0
Dim totalRecords As Integer = ds.Tables(0).Rows.Count
Do While itemRead < maxinumRows - 1 And currentIndex < totalRecords - 1
Dim question As New TBL_QUESTIONZ
question.QUESTIONCLASSID = ds.Tables(0).Rows(currentIndex).Item(0)
question.QUESTIONTYPE = ds.Tables(0).Rows(currentIndex).Item(1)
question.QUESTIONCONTENT = ds.Tables(0).Rows(currentIndex).Item(2)
question.QANALYSE = ds.Tables(0).Rows(currentIndex).Item(3)
results.Add(question)
itemRead += 1
currentIndex += 1
Loop
Return results
End Function
Public Shared Function GetQuestionsCount() As Integer
Dim sql As String = "select count(*) from TBL_QUESTION"
Dim conn As New SqlClient.SqlConnection("Server=lz-dbs01;UID=sa;PWD=password;Database=kddi;Connect Timeout=7200")
Dim cmb As New SqlClient.SqlCommand
cmb.CommandText = sql
cmb.Connection = conn
conn.Open()
Dim count As Integer = CType(cmb.ExecuteScalar, Integer)
conn.Close()
Return count
End Function
二、用户界面层中的实现
<asp:GridView ID="GridView1" runat="server" DataSourceID="ObjectDataSource1" Style="position: relative" AllowPaging="True" AutoGenerateColumns="False">
<Columns>
<asp:BoundField DataField="QDIFFICULTY" HeaderText="QDIFFICULTY" SortExpression="QDIFFICULTY" />
<asp:BoundField DataField="QUESTIONID" HeaderText="QUESTIONID" SortExpression="QUESTIONID" />
<asp:BoundField DataField="QANALYSE" HeaderText="QANALYSE" SortExpression="QANALYSE" />
<asp:BoundField DataField="QUESTIONCONTENT" HeaderText="QUESTIONCONTENT" SortExpression="QUESTIONCONTENT" />
<asp:BoundField DataField="RESULT" HeaderText="RESULT" SortExpression="RESULT" />
<asp:BoundField DataField="QUESTIONTYPE" HeaderText="QUESTIONTYPE" SortExpression="QUESTIONTYPE" />
<asp:BoundField DataField="QUESTIONCLASSID" HeaderText="QUESTIONCLASSID" SortExpression="QUESTIONCLASSID" />
<asp:BoundField DataField="FOUNDER" HeaderText="FOUNDER" SortExpression="FOUNDER" />
<asp:BoundField DataField="QUESTIONSTATE" HeaderText="QUESTIONSTATE" SortExpression="QUESTIONSTATE" />
<asp:BoundField DataField="FOUNDDATE" HeaderText="FOUNDDATE" SortExpression="FOUNDDATE" />
<asp:BoundField DataField="WAIT1" HeaderText="WAIT1" SortExpression="WAIT1" />
<asp:BoundField DataField="WAIT3" HeaderText="WAIT3" SortExpression="WAIT3" />
<asp:BoundField DataField="WAIT2" HeaderText="WAIT2" SortExpression="WAIT2" />
<asp:BoundField DataField="WAIT5" HeaderText="WAIT5" SortExpression="WAIT5" />
<asp:BoundField DataField="WAIT4" HeaderText="WAIT4" SortExpression="WAIT4" />
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="GetQUESTION"
TypeName="framework.business.M03QuestionManageAction" ConflictDetection="CompareAllValues" SelectCountMethod="GetQuestionsCount" StartRowIndexParameterName="startRowIndex" MaximumRowsParameterName="maxinumRows" EnablePaging="True">
</asp:ObjectDataSource>