[GridView控件]自定义分页

     <tr>
       
<td align="center" valign="middle" ><asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                    Width
="600px" PageSize="10"  OnRowDataBound="GridView1_RowDataBound">
                                    
<Columns>
                                        
<asp:TemplateField HeaderText="序号">
                                        
<ControlStyle Width="10%" />
                                        
<ItemStyle Width="10%" />
                                          
<ItemTemplate>
                                            
<%#(Container.DataItemIndex+1).ToString() %>                                           
                                           
</ItemTemplate>
                                        
</asp:TemplateField>
                                        
<asp:BoundField DataField="ID1" HeaderText="ID" Visible="false" />
                                        
<asp:BoundField DataField="NewsTitle1" HeaderText="新闻标题" SortExpression="NewsTitle1" >
                                            
<ControlStyle Font-Underline="True" />
                                            
<ItemStyle HorizontalAlign="Left" Width="56%" />
                                            
<FooterStyle Font-Strikeout="False" HorizontalAlign="Left" />
                                        
</asp:BoundField>
                                        
<asp:BoundField DataField="NewsDateTime1" HeaderText="时间" SortExpression="NewsDateTime1">
                                            
<ItemStyle Width="12%" />                                        </asp:BoundField>
                                           
<asp:BoundField DataField="ItemID1" HeaderText="ItemID1" Visible="false" />
                                    
</Columns>
                                    
<EmptyDataTemplate>
                                    该栏目暂时没有数据
                                    
</EmptyDataTemplate>
                                
<PagerStyle BackColor="Transparent" BorderColor="White" ForeColor="DarkGray" />
                                
<PagerSettings Visible="false" />
                          
</asp:GridView>
                          
<table cellpadding="0" cellspacing="0" border="0">
                          
<tr>
                    
<td align="center" style="height: 25px; width: 569px;">
                        
<asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick"
                            runat
="server">首 页</asp:LinkButton>&nbsp;<asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上一页</asp:LinkButton>
                        
<asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下一页</asp:LinkButton>
                        
<asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾 页</asp:LinkButton>
                        
<asp:Label ID="LblCurrentIndex" runat="server"></asp:Label>
                        
<asp:Label ID="LblPageCount" runat="server"></asp:Label>
                        
<asp:Label ID="LblRecordCount" runat="server"></asp:Label>
跳转到第
<asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> 
</asp:DropDownList></td>                <tr></table>
                          
</td>
     
</tr>


 

 

private void BindGrid()
    
{
//绑定GRIDVIEW
        string sql = "";
        News2006.Conn1 con 
= new News2006.Conn1();
        System.Data.SqlClient.SqlConnection con1 
= new System.Data.SqlClient.SqlConnection(con.sqlconnstr());
        
if (ItemBool(int.Parse(Session["item"].ToString())))
        
{
            sql 
= "";
        }

        
else
        
{
            sql 
= "";
        }

        System.Data.SqlClient.SqlCommand mycommand1 
= new System.Data.SqlClient.SqlCommand(sql, con1);
        mycommand1.Parameters.Add(
"@par1", System.Data.SqlDbType.Int, 4).Value = int.Parse(item)
        System.Data.SqlClient.SqlDataAdapter ada 
= new System.Data.SqlClient.SqlDataAdapter(mycommand1);
        con1.Open();
        System.Data.DataSet ds 
= new DataSet();
        ada.Fill(ds, 
"News1");
        
this.GridView1.DataSource = ds.Tables["News1"].DefaultView;
        
this.GridView1.DataBind();

        LblCurrentIndex.Text 
= "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
        LblPageCount.Text 
= "共 " + GridView1.PageCount.ToString() + " 页";
        LblRecordCount.Text 
= "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";
        
if (ds.Tables[0].Rows.Count == 0)
        
{
            btnFirst.Visible 
= false;
            btnPrev.Visible 
= false;
            btnNext.Visible 
= false;
            btnLast.Visible 
= false;

            LblCurrentIndex.Visible 
= false;
            LblPageCount.Visible 
= false;
            LblRecordCount.Visible 
= false;
        }

        
else if (GridView1.PageCount == 1)
        
{
            btnFirst.Visible 
= false;
            btnPrev.Visible 
= false;
            btnNext.Visible 
= false;
            btnLast.Visible 
= false;
        }


        
// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
        btnFirst.CommandName = "1";
        btnPrev.CommandName 
= (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());

        btnNext.CommandName 
= (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
        btnLast.CommandName 
= GridView1.PageCount.ToString();

        
this.ddlCurrentPage.Items.Clear();
        
for (int i = 1; i <= this.GridView1.PageCount; i++)
        
{
            
this.ddlCurrentPage.Items.Add(i.ToString());
        }

        
this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex;
        con1.Close();
    }

    
protected void PagerButtonClick(object sender, EventArgs e)
    
{
        GridView1.PageIndex 
= Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
        BindGrid();
    }

  

该分页除了首页,上一页,下一页,未页外还有一个Dropdownlist.......代码都帖出来了.....自己细细的看
posted @ 2007-07-23 15:52  暮江独钓  阅读(366)  评论(1编辑  收藏  举报