【转自】http://www.cnblogs.com/Jinglecat/archive/2006/06/13/424794.html

 

 

GridView较之DataGrid提供了更加强大、更加完善的功能,而且具备了丰富的可扩展功能,可以使用GridView提供的pagertemplate自定义分页模板:

事实上,GridView默认的几中分页样式,都是将相关按钮的CommandName设为Page,而CommandArgument设为相关参 数,可接受的参数包括,first,last,prev,next,<PageIndex>(具体数字),然后按事件回溯,触发顶层的 RowCommand,因此我们页可以使用这些默认的可识别的参数自定义自己的分页模板,asp.net会自动设置当前的NewPageIndex,而不 需要任何的冗余代码。

.aspx页面:

【转】自定义GridView分页模板<asp:gridview id="GridView1" runat="server" allowpaging="True" pagesize="10"
【转】自定义GridView分页模板            autogeneratecolumns
="False" datasourceid="SqlDataSource1"
【转】自定义GridView分页模板            onpageindexchanging
="GridView1_PageIndexChanging">
【转】自定义GridView分页模板            
<columns>
【转】自定义GridView分页模板                
<asp:boundfield datafield="CompanyName" headertext="CompanyName" sortexpression="CompanyName" />
【转】自定义GridView分页模板                
<asp:boundfield datafield="ContactTitle" headertext="ContactTitle" sortexpression="ContactTitle" />
【转】自定义GridView分页模板                
<asp:boundfield datafield="Phone" headertext="Phone" sortexpression="Phone" />
【转】自定义GridView分页模板                
<asp:boundfield datafield="Fax" headertext="Fax" sortexpression="Fax" />
【转】自定义GridView分页模板                
<asp:boundfield datafield="ContactName" headertext="ContactName" sortexpression="ContactName" />
【转】自定义GridView分页模板            
</columns>
【转】自定义GridView分页模板                       
<pagertemplate>
【转】自定义GridView分页模板                        
<table width="100%">
【转】自定义GridView分页模板                          
<tr>
【转】自定义GridView分页模板                            
<td style="text-align:right">
【转】自定义GridView分页模板                            第
<asp:Label id="lblPageIndex" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />页
【转】自定义GridView分页模板                                共/
<asp:Label id="lblPageCount" runat="server" text='<%# ((GridView)Container.Parent.Parent).PageCount  %>' />页 
【转】自定义GridView分页模板                                
<asp:linkbutton id="btnFirst" runat="server" causesvalidation="False" commandargument="First" commandname="Page" text="首页" />
【转】自定义GridView分页模板                              
<asp:linkbutton id="btnPrev" runat="server" causesvalidation="False" commandargument="Prev" commandname="Page" text="上一页" />
【转】自定义GridView分页模板                             
<asp:linkbutton id="btnNext" runat="server" causesvalidation="False" commandargument="Next" commandname="Page" text="下一页" />                          
【转】自定义GridView分页模板                             
<asp:linkbutton id="btnLast" runat="server" causesvalidation="False" commandargument="Last" commandname="Page" text="尾页" />                                            
【转】自定义GridView分页模板                             
<asp:textbox id="txtNewPageIndex" runat="server" width="20px" text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1  %>' />
【转】自定义GridView分页模板                             
<asp:linkbutton id="btnGo" runat="server" causesvalidation="False" commandargument="-1" commandname="Page" text="GO" /><!-- here set the CommandArgument of the Go Button to '-1' as the flag -->
【转】自定义GridView分页模板                             
</td>
【转】自定义GridView分页模板                          
</tr>
【转】自定义GridView分页模板                        
</table>
【转】自定义GridView分页模板                    
</pagertemplate>
【转】自定义GridView分页模板        
</asp:gridview> 
【转】自定义GridView分页模板    
【转】自定义GridView分页模板    
【转】自定义GridView分页模板        
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"
【转】自定义GridView分页模板            providername
="System.Data.SqlClient" selectcommand="SELECT [CompanyName], [ContactTitle], [Phone], [Fax], [ContactName] FROM [Customers]">
【转】自定义GridView分页模板        
</asp:sqldatasource>

PageIndexChanging处理程序:
【转】自定义GridView分页模板    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
 refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用
TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006年6月21日3:15:33
    }

注意到,上面的示例中,由于增加了一个跳转按钮GO,但是asp。net不支持相关的CommandArgument值,虽然可以将Go Button的Commandname设为Page,还需要手动的在PageIndexChanging增加部分处理逻辑。

 

相关文章:

  • 2021-10-18
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
  • 2021-10-02
猜你喜欢
  • 2021-11-03
  • 2022-12-23
  • 2022-12-23
  • 2021-11-25
  • 2021-12-01
相关资源
相似解决方案