
Figure 5 ClientPageDataGrid
ClientPageDataGrid的开发接口包括4个公共属性和其他从DataGrid继承来的属性(如Figure
6所示)。AllowClientPaging属性设置是否允许翻页,默认为允许。因此你可以在设置ClientPageDataGrid时略过AllowClientPaging=true。ClientPageSize属性设置每页显示的记录条数,ClientPageCount获得页面的总数。ClientCurrentPageIndex设置当先哪页被显示。下面的Page_Load方法设置名字为MyDataGrid的ClientPageDataGrid控件显示第2页(索引0表示第1),而不是第1页。 逆风者
void Page_Load (Object sender, EventArgs e)
{
if (!IsPostBack) {
BindDataGrid ();
MyDataGrid.ClientCurrentPageIndex = 1;
}
}
当ClientPageDataGrid所在页面的请求发送到服务器后,页面返回后,ClientCurrentPageIndex属性就被更新,来指示显示哪一页。服务器并不知道用户在看哪一页。
ClientPageDataGrid详情
ClientPageDataGrid输出标页数的HTML表格从使用Render方法开始,如Figure
7所示。你可以通过本文上面的链接下载完整的源代码。Render是一个从System.Web.UI.Control继承来的一个虚方法。在Microsoft?
.NET Framework中,当包括该控件的页面被请求时,Render方法被用来将控件加入到HTML中去。
ClientPageDataGrid重写了DataGrid的方法并且多次调用基类的实现,而不是一次。特别说明的是,ClientPageDataGrid在输出每一页时调用DataGrid.Render,同时通过对RenderBeginTag和RenderEndTag的调用将输出加入到<DIV>模块中。在调用基类的Render方法前,ClientPageDataGrid.Render调用一个本地的ShowItems方法来隐藏那些在页面上不显示的记录,方法是设置那些行的Visible属性为否(见Figure
8)。
当ClientPageDataGrid被设置为每页显示16条记录,也就意味着第1个<DIV>包括该表格的前16条记录,第2个<DIV>包括该表格紧接着的16条记录,以此类推。除1个之外所有的<DIV>模块通过设置样式
style=”display:none”来隐藏。只有显示页的索引等于ClientCurrentPageIndex时,<DIV>模块才被显示,它的样式被设置为style=”display:block”。
ClientPageDataGrid中的每一页包括一个分页器,该分页器存在于DataGrid当中,因为ClientPageDataGrid重写了DataGrid的DataBind方法,并且通过设置AllowPaging为真、PageSize设置为每页显示条数来调用。ClientPageDataGrid.cs中大部分代码就是来使页面正常工作,确保支持Previous/next-style分页器(<PagerStyle
Mode="NextPrev">)和数字分页器(<PagerStyle Mode="NumericPages">)。
传统的包括LinkButtons的DataGrid分页器,发送页面请求到服务器,然后产生PageIndexChanged事件。ClientPageDataGrid用指向客户端的JavaScript函数的链接取代了这些LinkButtons。UpdatePager方法(如Figure
9所示),在Render调用基类的Render方法之前被取代。UpdatePager通过在DataGrid中搜索类型为ListItemType.Pager的行,来找到要显示的页面,接着就删除页面中的控件,加入需要的控件。
下面是一个传统的DataGrid,触发分页器时输出HTML的例子:
<tr>
<td colspan="3">
<a href= "javascript:__doPostBack(''MyDataGrid$_ctl20$_ctl0'','''')">
<</a>
<a href="javascript:__doPostBack(''MyDataGrid$_ctl20$_ctl1'','''')">
></a>
</td>
</tr> 本文章更多内容:<<上一页 - 1 - 2 - 3 - 4 - 下一页>> |