gridview 万能分页代码

实现如下图片的分页:第【】页,共【】页,首页,上一页,下一页,末页 跳到某页;图片在最下面;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();
    }

 

 

 

知识点1

CausesValidation="False" -----不执行当前页的验证

还有种不执行验证的方法是ValidationGroup="group1" 给控件进行分组

这种方法在页面验证用的比较多

知识点2

Container.Parent.Parent 绑定数据性能最好

(GridView)Container.Parent.Parent).PageIndex 的联想!!!

DataBinder.Eval(Container.DataItem,"Name")和Container.DataItem("Name")有什么区别?

DataBinder是System.Web里面的一个静态类,它提供了Eval方法用于简化数据绑定表达式的编写,但是它使用的方式是通过Reflection等开销比较大的方法来达到易用性,因此其性能并不是最好的。而Container则根本不是任何一个静态的对象或方法,它是ASP.NET页面编译器在数据绑定事件处理程序内部声明的局部变量,其类型是可以进行数据绑定的控件的数据容器类型(如在Repeater内部的数据绑定容器叫RepeaterItem),在这些容器类中基本都有DataItem属性,因此你可以写Container.DataItem,这个属性返回的是你正在被绑定的数据源中的那个数据项。如果你的数据源是DataTable,则这个数据项的类型实际是DataRowView。

http://www.qqread.com/aspdotnet/u490415.html

知识点3:

如何正确理解ASP.NET中的Sender

//当前实例


举个例子:
<input type="submit" id="a1" value="
" OnServerClick="Submit_Click" runat="server">
<input type="submit" id="a2" value="
" OnServerClick="Submit_Click" runat="server">
<input type="submit" id="a3" value="
" OnServerClick="Submit_Click" runat="server">
<input type="submit" id="a4" value="
" OnServerClick="Submit_Click" runat="server">
这是四个Submit 按钮,我们知道它触发的事件是同一个事件,但要怎么来区分呢?看看下面就知道了:
public void Submit_Click(Object sender, EventArgs E)
{
switch (((Control)sender).ID)
{
case "a1" :showmsg.Text=""
宋颜浩; break;
case "a2" :showmsg.Text=""
李雁冰; break;
case "a3" :showmsg.Text=""
费文华; break;
case "a4" :showmsg.Text=""
吴晓桃; break;
}
}
</script>
<asp:Label id=showmsg runat=server />

 

知识点4:

当单击页面的连接的时候连接到这个连接的页面,这个连接来自数据库

我用的是ViewState,循环绑定实现的,一下是实现的代码:

 

protected void gvgetall_RowDataBound(object sender, GridViewRowEventArgs e)

    {

 

        if (e.Row.RowType == DataControlRowType.DataRow)

        {

            Label hlDownload = (Label)e.Row.FindControl("lbweb");

            string lbweb = hlDownload.Text.Trim().ToString();

           ViewState["web"] = lbweb;        

          

        }

知识点5:

正则表达式:

只可以输入数字 onkeyup="value=value.replace(/[^\d]/g,'') "

 

 

posted @ 2010-03-26 16:45  红萝卜  阅读(1675)  评论(3编辑  收藏  举报