一、特点:
可以用在母板、MS Ajax等控件嵌套的任何地方,支持GridView,Repeater等数据控件的分页。
二、属性:
1. PagerStyle: 设置分页样式(NextPrev,NumericPages)
2. ControlToPaginate: 指定要实现分页的控件ID(数据控件本身不需要进行数据绑定了,只需对WebPager进行数据绑定就行了)
3. PageSize: 获取或设置要在单页上显示的项数
4. CurrentPageIndex: 获取或设置当前页的索引
三、事件:
1. OnPageIndexChanged: 执行分页时触发的分页事件
四、源码下载
五、示例: (Repeater用法于此类似)
<
asp:GridView
ID
="GridView1"
runat
="server"
Width
="100%"
AutoGenerateColumns
="true"
OnRowDataBound ="GridView1_RowDataBound" AllowSorting ="True" OnSorting ="GridView1_Sorting" >
< PagerSettings Visible ="False" />
</ asp:GridView >
< cc1:WebPager ID ="WebPager1" runat ="server" PagerStyle ="NextPrev" ControlToPaginate ="GridView1"
PageSize ="5" OnPageIndexChanged ="WebPager1_PageIndexChanged" />
OnRowDataBound ="GridView1_RowDataBound" AllowSorting ="True" OnSorting ="GridView1_Sorting" >
< PagerSettings Visible ="False" />
</ asp:GridView >
< cc1:WebPager ID ="WebPager1" runat ="server" PagerStyle ="NextPrev" ControlToPaginate ="GridView1"
PageSize ="5" OnPageIndexChanged ="WebPager1_PageIndexChanged" />
Page事件
#region Page事件
Workflow.BLL.Workflow myBiz = new Workflow.BLL.Workflow();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetData();//重新获取操作后的数据源
BindGrid();//绑定GridView,为删除服务
}
}
#endregion
Gridview ---------------------------------------------------------- #region Gridview ----------------------------------------------------------
数据绑定#region 数据绑定
/**//// <summary>
/// 获取数据源
/// </summary>
private void GetData()
{
this.DataSource = myBiz.GetFlowDoing(Framework.Globals.CurrentUser.UserID).Tables[0];
}
/**//// <summary>
/// 初始化绑定
/// </summary>
private void BindGrid()
{
WebPager1.DataSource = this.DataSource;
WebPager1.DataBind();
}
#endregion
排序#region 排序
/**//// <summary>
/// 排序
/// </summary>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortDirection = "";
string sortExpression = e.SortExpression;
if (this.Sort_Direction == SortDirection.Ascending)
{
this.Sort_Direction = SortDirection.Descending;
sortDirection = "DESC";
}
else
{
this.Sort_Direction = SortDirection.Ascending;
sortDirection = "ASC";
}
DataView Source = new DataView(this.DataSource);
Source.Sort = e.SortExpression + " " + sortDirection;
//GridView1.DataSource = Source;//不能再用此绑定,否则影响排序后的分页。
//GridView1.DataBind();
this.DataSource = Source.ToTable();//重新设置数据源,绑定
BindGrid();
}
#endregion
分页#region 分页
protected void WebPager1_PageIndexChanged(object sender, wf.WebPager.PageChangedEventArgs e)
{
WebPager1.CurrentPageIndex = e.NewPageIndex;
WebPager1.DataSource = this.DataSource;
WebPager1.DataBind();
}
#endregion
#endregion
Workflow.BLL.Workflow myBiz = new Workflow.BLL.Workflow();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GetData();//重新获取操作后的数据源
BindGrid();//绑定GridView,为删除服务
}
}
#endregion
Gridview ---------------------------------------------------------- #region Gridview ----------------------------------------------------------
数据绑定#region 数据绑定
/**//// <summary>
/// 获取数据源
/// </summary>
private void GetData()
{
this.DataSource = myBiz.GetFlowDoing(Framework.Globals.CurrentUser.UserID).Tables[0];
}
/**//// <summary>
/// 初始化绑定
/// </summary>
private void BindGrid()
{
WebPager1.DataSource = this.DataSource;
WebPager1.DataBind();
}
#endregion
排序#region 排序
/**//// <summary>
/// 排序
/// </summary>
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
string sortDirection = "";
string sortExpression = e.SortExpression;
if (this.Sort_Direction == SortDirection.Ascending)
{
this.Sort_Direction = SortDirection.Descending;
sortDirection = "DESC";
}
else
{
this.Sort_Direction = SortDirection.Ascending;
sortDirection = "ASC";
}
DataView Source = new DataView(this.DataSource);
Source.Sort = e.SortExpression + " " + sortDirection;
//GridView1.DataSource = Source;//不能再用此绑定,否则影响排序后的分页。
//GridView1.DataBind();
this.DataSource = Source.ToTable();//重新设置数据源,绑定
BindGrid();
}
#endregion
分页#region 分页
protected void WebPager1_PageIndexChanged(object sender, wf.WebPager.PageChangedEventArgs e)
{
WebPager1.CurrentPageIndex = e.NewPageIndex;
WebPager1.DataSource = this.DataSource;
WebPager1.DataBind();
}
#endregion
#endregion