GridView的翻页

网上找的资料
属性
AllowPaging="True" PageSize="15"
前台

<PagerTemplate>
    
<table width="100%" style="font-size:12px;">
        
<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>&nbsp;&nbsp;
            
<asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False"
                CommandName
="Page" Text="首页" CommandArgument="first" OnClick="btnFirst_Click">
            
</asp:LinkButton>
            
<asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False"
                CommandName
="Page" Text="上一页" CommandArgument="prev" onclick="btnFirst_Click">
            
</asp:LinkButton>
            
<asp:LinkButton ID="btnNext" runat="server" CausesValidation="False"
                CommandName
="Page" Text="下一页" CommandArgument="next" OnClick="btnFirst_Click">
            
</asp:LinkButton>
            
<asp:LinkButton ID="btnLast" runat="server" CausesValidation="False"
                CommandName
="Page" Text="尾页" CommandArgument="last" OnClick="btnFirst_Click">
            
</asp:LinkButton>
            
<asp:TextBox ID="txtNewPageIndex" runat="server" Text='<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>'
                Width="20px" AutoPostBack="True" 
                ontextchanged="txtNewPageIndex_TextChanged">
</asp:TextBox>
            
<asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="go"
                CommandName
="Page" Text="GO" OnClick="btnFirst_Click"></asp:LinkButton>
        
</td>
        
</tr>
    
</table>
</PagerTemplate>

后台
protected void btnFirst_Click(object sender, EventArgs e)
        {
            
try
            {
                
switch (((LinkButton)sender).CommandArgument.ToString())
                {
                    
case "first":
                        gdvck.PageIndex 
= 0;
                        
break;
                    
case "last":
                        gdvck.PageIndex 
= gdvck.PageCount - 1;
                        
break;
                    
case "prev":
                        gdvck.PageIndex 
= gdvck.PageIndex - 1;
                        
break;
                    
case "next":
                        gdvck.PageIndex 
= gdvck.PageIndex + 1;
                        
break;
                    
case "go":
                        {
                            GridViewRow gvr 
= gdvck.BottomPagerRow;
                            TextBox temp 
= (TextBox)gvr.FindControl("txtNewPageIndex");
                            
int res = Convert.ToInt32(temp.Text.ToString());
                            gdvck.PageIndex 
= res - 1;
                        }
                        
break;
                }
                ImageButton1_Click(
nullnull);  //重绑
            }
            
catch (Exception ex)
            {
                lblmessage.Text 
= "请输入1-9999自然数 -" + ex.Message.ToString();
            }
        }

        
protected void txtNewPageIndex_TextChanged(object sender, EventArgs e)
        {
            
try
            {
                GridViewRow gvr 
= gdvck.BottomPagerRow;
                TextBox temp 
= (TextBox)gvr.FindControl("txtNewPageIndex");
                
int res = Convert.ToInt32(temp.Text.ToString());
                gdvck.PageIndex 
= res - 1;
                ImageButton1_Click(
nullnull);
            }
            
catch (Exception ex)
            {
                lblmessage.Text 
= "请输入1-9999自然数 -" + ex.Message.ToString();
            }
        }


posted on 2009-08-14 09:56  马英超  阅读(264)  评论(0编辑  收藏  举报