自定义GridView分页模板

自定义GridView分页模板

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

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

.aspx页面:

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


PageIndexChanging处理程序:

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView theGrid 
= sender as GridView;  // refer to the GridView
        int newPageIndex = 0;

        
if (-2 == e.NewPageIndex) // when click the "GO" Button
            TextBox txtNewPageIndex = null;
            //GridViewRow pagerRow 
= theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count - 1as GridViewRow; // refer to PagerTemplate
GridViewRow pagerRow = theGrid.BottomPagerRow; //GridView较DataGrid提供了更多的API,获取分页块可以使用
BottomPagerRow 或者TopPagerRow,当然还增加了HeaderRow和FooterRow
//updated at 2006年6月21日3:15:33

            
if (null != pagerRow) {
                txtNewPageIndex 
= pagerRow.FindControl("txtNewPageIndex"as TextBox;   // refer to the TextBox with the NewPageIndex value
            }


            
if (null != txtNewPageIndex) {
                newPageIndex 
= int.Parse(txtNewPageIndex.Text) - 1// get the NewPageIndex
            }

        }

        
else {  // when click the first, last, previous and next Button
            newPageIndex = e.NewPageIndex;
        }


        
// check to prevent form the NewPageIndex out of the range
        newPageIndex = newPageIndex < 0 ? 0 : newPageIndex;
        newPageIndex 
= newPageIndex >= theGrid.PageCount ? theGrid.PageCount - 1 : newPageIndex;

        
// specify the NewPageIndex
        theGrid.PageIndex = newPageIndex;

        
// rebind the control
        
// in this case of retrieving the data using the xxxDataSoucr control,
        
// just do nothing, because the asp.net engine binds the data automatically
    }

 

protected void btnGo_Click(object sender, EventArgs e)
    {
        GridViewRow grdRow = this.gvlist.BottomPagerRow;
        try
        {
            TextBox txt_NewPageIndex = grdRow.FindControl("txt_NewPageIndex") as TextBox;
            if (txt_NewPageIndex.Text.Trim().Length > 0)
            {
                this.gvlist.PageIndex = Convert.ToInt32(txt_NewPageIndex.Text.Trim())-1;
                BindData(StrProCode);
            }
        }
        catch
        {
            //Response.Write("<script>alert('页数请填写数字!')</script>");
        }

    }


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

 

当只有一页时分页不显示问题解决:

GridView 的 Controls 集合中包括了 Header、DataRow、Footer、Pager等等所有可以显示的行,而且不管有没有显示,这些行都包含在 Controls 集合中,只不过仅有一页数据时,Pager 行的 Visible 属性被设置为了 false。既然如此,只要在数据绑定结束后把 Pager 行的 Visible 重新设置为 true 不就解决问题了吗?下面是解决方法:

 

    public static void gridView_DataBound(object sender, EventArgs e)
    {
        if (gridView.Rows.Count != 0)
        {
            Control table = gridView.Controls[0];
            int count = table.Controls.Count;
            table.Controls[count - 1].Visible = true;
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
GridView是一个非常常用的ASP.NET控件,它可以方便地呈现数据,并且支持分页功能。在GridView中,可以使用内置的分页样式,也可以自定义分页样式。下面我将介绍两种自定义分页样式。 1. 自定义分页样式一:使用自定义模板 GridView提供了一个PagerTemplate属性,可以使用这个属性来自定义分页模板。下面是一个简单的例子: ```html <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" /> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Age" HeaderText="Age" /> </Columns> <PagerTemplate> <div class="pagination"> <span>共<%# GridView1.PageCount %>页,当前第<%# GridView1.PageIndex+1 %>页</span> <ul> <li><a href="#" onclick="javascript:__doPostBack('GridView1','Page', '<%= GridView1.PageIndex-1 %>')">上一页</a></li> <% for (int i = 0; i < GridView1.PageCount; i++) { %> <li <% if (GridView1.PageIndex == i) { %>class="active"<% } %>><a href="#" onclick="javascript:__doPostBack('GridView1','Page', '<%= i %>')"><%= i+1 %></a></li> <% } %> <li><a href="#" onclick="javascript:__doPostBack('GridView1','Page', '<%= GridView1.PageIndex+1 %>')">下一页</a></li> </ul> </div> </PagerTemplate> </asp:GridView> ``` 在这个例子中,我们使用PagerTemplate属性定义了一个分页模板。该模板包含一个div元素,用于包含分页控件。在div元素中,我们使用了一些HTML代码和ASP.NET表达式来生成分页控件。 在分页控件中,我们使用了一个ul元素和一些li元素来呈现页码。在每个li元素中,我们使用了一个a元素和一个onclick事件来触发GridView分页功能。在onclick事件中,我们使用了一个__doPostBack函数来向服务器发送分页请求,并且传递了两个参数:GridView的ID和分页索引。 2. 自定义分页样式二:使用第三方分页插件 除了使用自定义模板之外,我们还可以使用第三方分页插件来自定义GridView分页样式。下面是一个使用Bootstrap Paginator插件的例子: ```html <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnPageIndexChanging="GridView1_PageIndexChanging"> <Columns> <asp:BoundField DataField="ID" HeaderText="ID" /> <asp:BoundField DataField="Name" HeaderText="Name" /> <asp:BoundField DataField="Age" HeaderText="Age" /> </Columns> </asp:GridView> <div id="pagination"></div> <script src="https://code.jquery.com/jquery-3.4.1.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-paginator/2.2.5/bootstrap-paginator.min.js"></script> <script> $(function () { $('#pagination').bootstrapPaginator({ currentPage: <%= GridView1.PageIndex+1 %>, totalPages: <%= GridView1.PageCount %>, onPageChanged: function (e, oldPage, newPage) { __doPostBack('GridView1', 'Page', newPage-1); } }); }); </script> ``` 在这个例子中,我们使用了Bootstrap Paginator插件来自定义分页样式。在页面加载完成之后,我们使用jQuery选择器选中了一个id为pagination的div元素,并且调用了bootstrapPaginator函数来初始化分页控件。在bootstrapPaginator函数中,我们传递了三个参数:当前页码、总页码数和一个回调函数。在回调函数中,我们使用__doPostBack函数向服务器发送分页请求,并且传递了两个参数:GridView的ID和分页索引。 这两种自定义分页样式都非常简单易懂,可以根据自己的需求进行选择和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值