[GridView控件]自定义分页

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


 

 

None.gifprivate void BindGrid()
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif
//绑定GRIDVIEW
InBlock.gif
        string sql = "";
InBlock.gif        News2006.Conn1 con 
= new News2006.Conn1();
InBlock.gif        System.Data.SqlClient.SqlConnection con1 
= new System.Data.SqlClient.SqlConnection(con.sqlconnstr());
InBlock.gif        
if (ItemBool(int.Parse(Session["item"].ToString())))
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sql 
= "";
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            sql 
= "";
ExpandedSubBlockEnd.gif        }

InBlock.gif        System.Data.SqlClient.SqlCommand mycommand1 
= new System.Data.SqlClient.SqlCommand(sql, con1);
InBlock.gif        mycommand1.Parameters.Add(
"@par1", System.Data.SqlDbType.Int, 4).Value = int.Parse(item)
InBlock.gif        System.Data.SqlClient.SqlDataAdapter ada 
= new System.Data.SqlClient.SqlDataAdapter(mycommand1);
InBlock.gif        con1.Open();
InBlock.gif        System.Data.DataSet ds 
= new DataSet();
InBlock.gif        ada.Fill(ds, 
"News1");
InBlock.gif        
this.GridView1.DataSource = ds.Tables["News1"].DefaultView;
InBlock.gif        
this.GridView1.DataBind();
InBlock.gif
InBlock.gif        LblCurrentIndex.Text 
= "第 " + (GridView1.PageIndex + 1).ToString() + " 页";
InBlock.gif        LblPageCount.Text 
= "共 " + GridView1.PageCount.ToString() + " 页";
InBlock.gif        LblRecordCount.Text 
= "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";
InBlock.gif        
if (ds.Tables[0].Rows.Count == 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            btnFirst.Visible 
= false;
InBlock.gif            btnPrev.Visible 
= false;
InBlock.gif            btnNext.Visible 
= false;
InBlock.gif            btnLast.Visible 
= false;
InBlock.gif
InBlock.gif            LblCurrentIndex.Visible 
= false;
InBlock.gif            LblPageCount.Visible 
= false;
InBlock.gif            LblRecordCount.Visible 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
else if (GridView1.PageCount == 1)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            btnFirst.Visible 
= false;
InBlock.gif            btnPrev.Visible 
= false;
InBlock.gif            btnNext.Visible 
= false;
InBlock.gif            btnLast.Visible 
= false;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
InBlock.gif
        btnFirst.CommandName = "1";
InBlock.gif        btnPrev.CommandName 
= (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString());
InBlock.gif
InBlock.gif        btnNext.CommandName 
= (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());
InBlock.gif        btnLast.CommandName 
= GridView1.PageCount.ToString();
InBlock.gif
InBlock.gif        
this.ddlCurrentPage.Items.Clear();
InBlock.gif        
for (int i = 1; i <= this.GridView1.PageCount; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.ddlCurrentPage.Items.Add(i.ToString());
ExpandedSubBlockEnd.gif        }

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

None.gif    
protected void PagerButtonClick(object sender, EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif{
InBlock.gif        GridView1.PageIndex 
= Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
InBlock.gif        BindGrid();
ExpandedBlockEnd.gif    }

None.gif  

该分页除了首页,上一页,下一页,未页外还有一个Dropdownlist.......代码都帖出来了.....自己细细的看

转载于:https://www.cnblogs.com/luck200803/archive/2007/07/23/828428.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值