GridView,Repeater分页控件:WebPager(开源)

来源:互联网 发布:apache ivy 编辑:程序博客网 时间:2024/05/21 18:36

 

一、特点:
可以用在母板、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" />

#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


#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


 

 
WebPager控件源码下载(保留版权)

 控件引用:
<%@ Register Assembly="wf.WebControls" Namespace="wf.WebPager" TagPrefix="cc1" %>

原创粉丝点击