gridview 万能分页代码

 2023-09-07 阅读 17 评论 0

摘要:实现如下图片的分页:第【】页,共【】页,首页,上一页,下一页,末页 跳到某页;图片在最下面;gv里面的模板PagerTemplate复制到你的gv对应位置,然后在 protected void gvgetall_PageIndexChanging(object sende

实现如下图片的分页:第【】页,共【】页,首页,上一页,下一页,末页 跳到某页;图片在最下面;gv里面的模板PagerTemplate复制到你的gv对应位置,然后在

 

protected void gvgetall_PageIndexChanging(object sender, GridViewPageEventArgs e)事件里面写.cs里代码就可以实现。 

 

<asp:GridView ID="gvgetall" runat="server" AllowSorting="True" AutoGenerateColumns="False"
                            Width
="100%" OnRowCommand="gvgetall_RowCommand" EmptyDataText="没有任何数据可以显示" AllowPaging="True"
                            OnPageIndexChanging
="gvgetall_PageIndexChanging" PageSize="2" OnSorting="gvgetall_Sorting"
                            OnRowCreated
="gvgetall_RowCreated" OnRowDataBound="gvgetall_RowDataBound">
                            
<Columns>
                                
<asp:BoundField DataField="En_id" HeaderText="编号" Visible="False">
                                    
<ItemStyle HorizontalAlign="Center" />
                                
</asp:BoundField>
                                
<asp:TemplateField HeaderText="企业名称" SortExpression="name">
                                    
<ItemTemplate>
                                        
<asp:LinkButton ID="btnupdate" CommandName="update" CommandArgument='<%#Eval("ID") %>'
                                            runat
="server" Text='<%# trimFieldLength(Eval("name").ToString())%>' ForeColor="blue" />
                                    
</ItemTemplate>
                                    
<ItemStyle HorizontalAlign="Center" />
                                    
<FooterTemplate>
                                        RETGRYTRYTYGTY
                                    
</FooterTemplate>
                                
</asp:TemplateField>
                                
<asp:TemplateField SortExpression="WEBSITE" HeaderText="企业网址">
                                    
<ItemTemplate>
                                        
<asp:Label ID="lbweb" Visible="false" runat="server" Text='<%# trimFieldLength1(Eval("WEBSITE").ToString())%>'></asp:Label>
                                        
<a href='<%=ViewState["web"]%>' target="_blank">
                                            
<%# trimFieldLength1(Eval("WEBSITE").ToString())%>
                                        
</a>
                                    
</ItemTemplate>
                                    
<ItemStyle HorizontalAlign="Center" />
                                
</asp:TemplateField>
                                
<asp:BoundField DataField="address" HeaderText="企业地址" SortExpression="address">
                                    
<ItemStyle HorizontalAlign="Center" />
                                
</asp:BoundField>
                            
</Columns>
                            
<PagerStyle CssClass="cssPager" />
                            
<AlternatingRowStyle BackColor="#E2F8FF"></AlternatingRowStyle>
                            
<HeaderStyle HorizontalAlign="Center" BackColor="#99CCFF"></HeaderStyle>
                            
<PagerTemplate>
                                
<table>
                                    
<tr>
                                        
<td style="text-align: right">
                                            第
<asp:Label ID="lblPageIndex" runat="server" Text="<%#((GridView)Container.Parent.Parent).PageIndex + 1 %>"></asp:Label>
                                            共
<asp:Label ID="lblPageCount" runat="server" Text="<%# ((GridView)Container.Parent.Parent).PageCount %>"></asp:Label>
                                            
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandArgument="First"
                                                CommandName
="Page" Text="首页" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>"></asp:LinkButton>
                                            
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandArgument="Prev"
                                                CommandName
="Page" Text="上一页" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=0 %>"></asp:LinkButton>
                                            
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandArgument="Next"
                                                CommandName
="Page" Text="下一页" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>"></asp:LinkButton>
                                            
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandArgument="Last"
                                                CommandName
="Page" Enabled="<%# ((GridView)Container.Parent.Parent).PageIndex!=((GridView)Container.Parent.Parent).PageCount-1 %>"
                                                Text
="尾页"></asp:LinkButton>
                                            
<asp:TextBox ID="txtNewPageIndex" runat="server" onkeyup="value=value.replace(/[^\d]/g,'') "
                                                Text
="<%# ((GridView)Container.Parent.Parent).PageIndex + 1%>" Width="20px"></asp:TextBox>
                                            
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="-1"
                                                CommandName
="Page" Text="GO"></asp:LinkButton>
                                        
</td>
                                    
</tr>
                                
</table>
                            
</PagerTemplate>
                        
</asp:GridView>


 .cs
protected void gvgetall_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {   

        GridView gvw 
= (GridView)sender;//当前实例
        if (e.NewPageIndex < 0)
        {
            TextBox pageNum 
= (TextBox)gvw.BottomPagerRow.FindControl("txtNewPageIndex");
            
int Pa = int.Parse(pageNum.Text);
            
if (Pa <= 0)
            {
                gvw.PageIndex 
= 0;
            }
            
else
            {
                gvw.PageIndex 
= Pa - 1;
            }
        }
        
else
        {
            gvw.PageIndex 
= e.NewPageIndex;
        }    

        bind();
    }

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/11139.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息