第一步:创建用于数据存取的类,放于App_Code目录下:
namespace CRM2006.ObjectDataSource
//命名空间,可写可不写,主要用于给objectdatasource控件的Type属性赋值
{
/// <summary>
///
/// </summary>
public class UserInfoHandle
{
public static DataTable GetUserEmployeeInfo(int StartRow, int PageSize)
//返回记录集的方法,类型可是DataSet,DataTable,Array等几种中的任一种,两个
//参数主要用于分页
{
int totalRecords;
int pageIndex = (int) StartRow/PageSize;
MembershipUserCollection users = Membership.GetAllUsers(pageIndex, PageSize, out totalRecords);
//执行查询,可以是任意形式的查询,只能返回一个集合就行了.
DataTable rtnTable = new DataTable();
rtnTable.Columns.Add("UserName");
rtnTable.Columns.Add("EmpNo");
foreach (MembershipUser user in users)
{
ProfileCommon p = new ProfileCommon();
p = p.GetProfile(user.UserName);
DataRow row = rtnTable.NewRow();
row[0] = user.UserName;
row[1] = p.Employee == null ? null : p.Employee.EmpNo;
rtnTable.Rows.Add(row);
}
return rtnTable;
}
//这个方法用于返回总记录条数,作用是给被绑定的控件提供一个接口,使得控
//件能够显示实际的分页情况
public static int SelectCount()
{
MembershipUserCollection users = Membership.GetAllUsers();
return users.Count;
}
}
}
第二步:创建一个页面,用于使用此数据源
<div style="width:100%;text-align:center">
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" CellPadding="4" DataSourceID="ObjectDataSource1"
ForeColor="#333333" GridLines="None" AllowPaging="True" PageSize="3" Width="300px">
<FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" CssClass="pager"/>
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
<PagerSettings FirstPageText="First Page" LastPageText="Last Page" Mode="NextPreviousFirstLast" />
</asp:GridView>
</div>
//MaximumRowsParameterName和StartRowIndexParameterName这两个
//属性是objectdatasource固有的属性,用于向SelectMethod属性指定的方法
//(即GetUserEmployeeInfo())传递参数,由于这个方法只用了两个参数,所以在这
//个示例中这样写就够了.如何还有第三甚至第四个参数,那么额外的参数就要在
//SelectParameters属性中指定.如下所示:
//<asp:ObjectDataSource ID="ObjectDataSource1" MaximumRowsParam
//eterName="PageSize" StartRowIndexParameterName="StartRow" Ena
//blePaging="true" SelectCountMethod="SelectCount" TypeName="CRM
//2006.ObjectDataSource.UserInfoHandle" SelectMethod="GetUserEmp
//loyeeInfo" runat="server"><SelectParameters><asp:Parameter
//Name="" DefaultValue="" /></SelectParameters>
//</asp:ObjectDataSource>
//再说一下这两个参数如何传递.MaximumRowsParameterName指定为
//PageSize,运行时,会自动把GridView的PageSize的值传递过去.
//StartRowIndexParameterName不一定非得指定为StartRow.
//但有一个原则,那就是这两个参数指定的名字必须同要调用的
//SelectMethod中的参数名保持完全一致.否则就会出错.
<asp:ObjectDataSource ID="ObjectDataSource1" MaximumRowsParameterName="PageSize" StartRowIndexParameterName="StartRow" EnablePaging="true" SelectCountMethod="SelectCount" TypeName="CRM2006.ObjectDataSource.UserInfoHandle" SelectMethod="GetUserEmployeeInfo"
runat="server"></asp:ObjectDataSource>
结束!