一个功能齐全的DataGrid分页例子[1]

[入库:2006年2月23日] [更新:2007年3月24日]

本文简介:

&lt;%@ Page language=&quot;c#&quot; EnableViewState = &quot;true&quot; Codebehind=&quot;DataGridPaging.aspx.cs&quot; AutoEventWireup=&quot;false&quot; Inherits=&quot;eMeng.Exam.DataGridPaging.DataGridPaging&quot; %&gt; &lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;&gt; &lt;HTML&gt; &lt;HEAD&gt; &lt;meta content=&quot;Visual Basic 7.0&quot; name=&quot;CODE_LANGUAGE&quot;&gt; &lt;meta content=&quot;JavaScript&quot; name=&quot;vs_defaultClientScript&quot;&gt; &lt;meta content=&quot;http://schemas.microsoft.com/intellisense/ie5&quot; name=&quot;vs_targetSchema&quot;&gt; &lt;/HEAD&gt; &lt;body MS_POSITIONING=&quot;GridLayout&quot;&gt; &lt;form id=&quot;Form1&quot; runat=&quot;server&quot;&gt; &lt;asp:datagrid id=&quot;MyDataGrid&quot; runat=&quot;server&quot; AutoGenerateColumns=&quot;False&quot; HorizontalAlign=&quot;Center&quot; AlternatingItemStyle-BackColor=&quot;#eeeeee&quot; HeaderStyle-BackColor=&quot;#aaaadd&quot; Font-Size=&quot;8pt&quot; Font-Name=&quot;Verdana&quot; CellSpacing=&quot;0&quot; CellPadding=&quot;3&quot; GridLines=&quot;Both&quot; BorderWidth=&quot;1&quot; BorderColor=&quot;black&quot; OnPageIndexChanged=&quot;MyDataGrid_Page&quot; PagerStyle-HorizontalAlign=&quot;Right&quot; PagerStyle-Mode=&quot;NumericPages&quot; PageSize=&quot;5&quot; AllowPaging=&quot;True&quot;&gt; &lt;AlternatingItemStyle BackColor=&quot;#EEEEEE&quot;&gt;&lt;/AlternatingItemStyle&gt; &lt;HeaderStyle BackColor=&quot;#AAAADD&quot; Font-Bold=&quot;True&quot; HorizontalAlign=&quot;Center&quot;&gt;&lt;/HeaderStyle&gt; &lt;PagerStyle HorizontalAlign=&quot;Right&quot; Mode=&quot;NumericPages&quot;&gt;&lt;/PagerStyle&gt; &lt;Columns&gt; &lt;asp:BoundColumn HeaderText=&quot;标题&quot; DataField=&quot;Title&quot; HeaderStyle-Width=&quot;480px&quot;&gt; &lt;/asp:BoundColumn&gt; &lt;asp:BoundColumn HeaderText=&quot;发表日期&quot; DataField=&quot;CreateDate&quot; DataFormatString=&quot;{0:yyyy-MM-dd hh:mm:ss}&quot;&gt; &lt;/asp:BoundColumn&gt; &lt;/Columns&gt; &lt;/asp:datagrid&gt; &lt;p style=&quot;FONT-SIZE:9pt&quot; align=&quot;center&quot;&gt; &lt;asp:label id=&quot;lblPageCount&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt;&amp;nbsp; &lt;asp:label id=&quot;lblCurrentIndex&quot; runat=&quot;server&quot;&gt;&lt;/asp:label&gt; &lt;asp:linkbutton id=&quot;btnFirst&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;0&quot;&gt;&lt;/asp:linkbutton&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnPrev&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;prev&quot;&gt;&lt;/asp:linkbutton&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnNext&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;next&quot;&gt;&lt;/asp:linkbutton&gt;&amp;nbsp; &lt;asp:linkbutton id=&quot;btnLast&quot; onclick=&quot;PagerButtonClick&quot; runat=&quot;server&quot; Font-Name=&quot;verdana&quot; Font-size=&quot;8pt&quot; ForeColor=&quot;navy&quot; CommandArgument=&quot;last&quot;&gt;&lt;/asp:linkbutton&gt; &lt;/p&gt; &lt;/form&gt; &lt;/body&gt; &lt;/HTML&gt;using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace eMeng.Exam.DataGridPaging { /// &lt;summary&gt; /// DataGridPaging 的摘要说明。 /// &lt;/summary&gt; public class DataGridPaging : System.Web.UI.Page { protected System.Web.UI.WebControls.DataGrid MyDataGrid; protected System.Web.UI.WebControls.Label lblPageCount; protected System.Web.UI.WebControls.Label lblCurrentIndex; protected System.Web.UI.WebControls.LinkButton btnFirst; protected System.Web.UI.WebControls.LinkButton btnPrev; protected System.Web.UI.WebControls.LinkButton btnNext; protected System.Web.UI.WebControls.LinkButton btnLast; private OleDbConnection cn = new OleDbConnection(); private void Page_Load(object sender, System.EventArgs e) { // 在此处放置用户代码以初始化页面 btnFirst.Text = &quot;最首页&quot;; btnPrev.Text = &quot;前一页&quot;; btnNext.Text = &quot;下一页&quot;; btnLast.Text = &quot;最后页&quot;; OpenDatabase(); BindGrid(); } private void OpenDatabase() { cn.ConnectionString = &quot;Provider=Microsoft.Jet.OLEDB.4.0;Data Source=&quot; + Server.MapPath(&quot;xxxx.mdb&quot;); cn.Open(); } private void ShowStats() { lblCurrentIndex.Text = &quot;第 &quot; + (MyDataGrid.CurrentPageIndex + 1).ToString() + &quot; 页&quot;; lblPageCount.Text = &quot;总共 &quot; + MyDataGrid.PageCount.ToString()</p> <div class="clear-both"></div> </div> <div class="pages"><a href=20060223162347911.htm target="_self" >首页</a> <a href=20060223162347911_2.htm target="_self">下页</a> <a href=20060223162347911_2.htm target="_self">尾页</a> <a href=20060223162347911.htm target="_self"><strong>[1]</strong></a> <a href=20060223162347911_2.htm target="_self">[2]</a> </div> <div class="keywords"> <strong>本文关键:</strong>一个功能齐全的DataGrid分页例子 </div> <div class="ad_doc_ad_2"> <script type="text/javascript"><!-- google_ad_client = "pub-1022332794981269"; google_ad_width = 728; google_ad_height = 90; google_ad_format = "728x90_as"; google_ad_type = "text_image"; //2007-03-23: 方案站728x90 google_ad_channel = "2351411502"; google_color_border = "FFFFFF"; google_color_bg = "FFFFFF"; google_color_link = "3D81EE"; google_color_text = "4C4C4C"; google_color_url = "6C82B5"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> <div class="clear-both"></div> </div> </div> <div id="content_right"> <div class="cbox" id="relate_doc"> <div class="darkboxTitle"><span>  相关方案</span></div> <div class="ad_site_index_right_1"> <!-- SiteSearch Google --> <form method="get" action="http://www.google.cn/custom" target="google_window"> <table border="0" bgcolor="#ffffff"> <tr><td nowrap="nowrap" valign="top" align="left" height="32"> <a href="http://www.google.com/"> <img src="http://www.google.com/logos/Logo_25wht.gif" border="0" alt="Google" align="middle"></img></a> <br/> <input type="hidden" name="domains" value="www.cn-doc.com"></input> <label for="sbi" style="display: none">输入您的搜索字词</label> <input type="text" name="q" size="15" maxlength="255" value="" id="sbi"></input> <label for="sbb" style="display: none">提交搜索表单</label> <input type="submit" name="sa" value="搜索" id="sbb"></input> </td></tr> <tr> <td nowrap="nowrap"> <table> <tr> <td> <input type="radio" name="sitesearch" value="" id="ss0"></input> <label for="ss0" title="搜索网络"><font size="-1" color="#000000">Web</font></label></td> <td> <input type="radio" name="sitesearch" value="www.cn-doc.com" checked id="ss1"></input> <label for="ss1" title="搜索 www.cn-doc.com"><font size="-1" color="#000000">www.cn-doc.com</font></label></td> </tr> </table> <input type="hidden" name="client" value="pub-1022332794981269"></input> <input type="hidden" name="forid" value="1"></input> <input type="hidden" name="channel" value="9722277709"></input> <input type="hidden" name="ie" value="GB2312"></input> <input type="hidden" name="oe" value="GB2312"></input> <input type="hidden" name="flav" value="0000"></input> <input type="hidden" name="sig" value="Dl1iwOxBnXTtaDIa"></input> <input type="hidden" name="cof" value="GALT:#008000;GL:1;DIV:#336699;VLC:663399;AH:center;BGC:FFFFFF;LBGC:336699;ALC:0000FF;LC:0000FF;T:000000;GFNT:0000FF;GIMP:0000FF;LH:50;LW:200;L:http://www.cn-doc.com/images/forgoogle.gif;S:http://www.cn-doc.com;FORID:1"></input> <input type="hidden" name="hl" value="zh-CN"></input> </td></tr></table> </form> <!-- SiteSearch Google --> </div> <ul class="darklist"> <li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162347564.htm" title="一个.net分页测试源码" target="_blank">一个.net分页测试源码</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162348773.htm" title="一个通过DataSet操作XML的类(原创)" target="_blank">一个通过DataSet操作XM</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162346130.htm" title="改进 ASP 应用程序中的字符串处理性能" target="_blank">改进 ASP 应用程序中的字符</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/2006022316234847.htm" title="WIN2000服务器安全配置" target="_blank">WIN2000服务器安全配置</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162348654.htm" title="Project Templates" target="_blank">Project Templat</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162348639.htm" title="学习PetShop3.0(5)生成定单" target="_blank">学习PetShop3.0(5)</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162348962.htm" title="ASP.NET中Web DataGrid的使用指南" target="_blank">ASP.NET中Web Dat</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162348650.htm" title="Checking a Range" target="_blank">Checking a Rang</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162348198.htm" title="WEB打印设置解决方案三(FileSystem组件实现WEB打印)" target="_blank">WEB打印设置解决方案三(Fi</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162348860.htm" title="亲密接触ASP.Net(6)" target="_blank">亲密接触ASP.Net(6)</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162347954.htm" title="NextRecordset 和 GetRows 双簧合奏" target="_blank">NextRecordset 和</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162347370.htm" title="ASP.NET简单整理收藏(1)" target="_blank">ASP.NET简单整理收藏(1</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162347813.htm" title="关于IFRAME 自适应高度的研究" target="_blank">关于IFRAME 自适应高度的</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162347617.htm" title="ASP.NET验证控件祥解" target="_blank">ASP.NET验证控件祥解</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/2006022316234767.htm" title="Repeater控件分页的例子" target="_blank">Repeater控件分页的例子</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162346960.htm" title="ASP.NET环境下,VB.NET语言,数据保存EXCEL模板操作" target="_blank">ASP.NET环境下,VB.N</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162346596.htm" title="ASP小偷(远程数据获取)程序的入门教程" target="_blank">ASP小偷(远程数据获取)程序</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162346426.htm" title="ASP.NET中实现直接从网页上下载文件,而不须引用文件URL来下载" target="_blank">ASP.NET中实现直接从网页</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162346444.htm" title="DataGrid学习五" target="_blank">DataGrid学习五</a>…</li><li><a href="../../_soft_asp_tech_doc/2006_02_23_16/20060223162346985.htm" title="RS.GETROWS使用详解" target="_blank">RS.GETROWS使用详解</a>…</li> </ul> <div class="ad_site_index_right_2"> <script type="text/javascript"><!-- google_ad_client = "pub-1022332794981269"; google_ad_width = 180; google_ad_height = 60; google_ad_format = "180x60_as_rimg"; google_cpa_choice = "CAAQ0cX8zwEaCEpGRZ0Cl8yyKPu_93M"; google_ad_channel = "6903800490"; //--> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script> </div> </div> </div> <div class="clear-both">&nbsp;</div> <div id="ContentBottom" class="darkBox"> <p>本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)</p><a href="#"><img src="../../images/btn_top.gif" width="51" height="11" alt="go top"/></a> </div> </div> </div> <script type="text/javascript" language="javascript" src="../../comm/doc/w3c_99_bottom.js"></script> <script type="text/javascript" language="javascript" src="../../comm/clicksum_doc.asp?typeid=45&infoid=472591"></script> <script type="text/javascript" language="JavaScript" src="../../comm/statistics.js"></script> </body> </html>