此分页方式与传统asp分页方式相仿。
datagridpage.aspx
以下为引用的内容: <%@ page language="c#" codebehind="datagridpage.aspx.cs" autoeventwireup="false" inherits="netcrm.datagridpage" %> <!doctype html public "-//w3c//dtd html 4.0 transitional//en" > <html> <head> <title>datagridpage</title> <meta name="generator" content="microsoft visual studio .net 7.1"> <meta name="code_language" content="c#"> <meta name="vs_defaultclientscript" content="javascript"> <meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5"> </head> <body ms_positioning="gridlayout"> <form id="form1" method="post" runat="server"> <asp:datagrid id="datagrid1" runat="server" alternatingitemstyle-backcolor="#eeeeee" headerstyle-backcolor="#aaaadd" font-size="8pt" font-name="verdana" cellpadding="3" borderwidth="1px" bordercolor="black" pagerstyle-horizontalalign="right" pagerstyle-mode="numericpages" pagesize="5" font-names="verdana" width="100%"> <alternatingitemstyle backcolor="#eeeeee"></alternatingitemstyle> <headerstyle backcolor="#aaaadd"></headerstyle> <pagerstyle horizontalalign="right" mode="numericpages"></pagerstyle> </asp:datagrid> </form> <table cellspacing="0" cellpadding="1" width="100%" bgcolor="#aaaadd" border="0"> <tbody> <tr> <td> <table cellspacing="0" cellpadding="4" width="100%" bgcolor="#fef8e2" border="0"> <tbody> <tr> <td class="m" nowrap align="center"><asp:literal id="literal1" runat="server"></asp:literal></td> </tr> <tr> <td class="c" nowrap align="center"><asp:literal id="literal2" runat="server"></asp:literal></td> </tr> </tbody> </table> </td> </tr> </tbody> </table> </body> </html> |
datagridpage.aspx.cs
以下为引用的内容: using system; using system.collections; using system.componentmodel; using system.data; using system.data.sqlclient; 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 netcrm { /// <summary> /// datagridpage 的摘要说明。 /// </summary> public class datagridpage : system.web.ui.page { protected system.web.ui.webcontrols.literal literal1; protected system.web.ui.webcontrols.literal literal2; protected system.web.ui.webcontrols.datagrid datagrid1; private void page_load(object sender, system.eventargs e) { // 在此处放置用户代码以初始化页面 if(!ispostback) { bindgrid(); } } private void bindgrid() { string connstring = "server=.;database=northwind;user id=sa;password=;"; string sql="select * from orders"; sqlconnection conn = new sqlconnection(connstring); conn.open(); dataset ds = new dataset(); sqldataadapter sqladapter = new sqldataadapter(sql,conn); sqladapter.fill(ds,"users"); dataview dataview = new dataview(); dataview = ds.tables[0].defaultview; datagrid1.datasource = ds.tables[0].defaultview; datagrid1.databind(); string cpage; int pagesize = 10; int currentpage; int pagecount; int numresults = 0; if (request.querystring["page"]==null) { cpage="1"; } else { cpage=request.querystring["page"].tostring(); } try { currentpage = int32.parse(cpage); } catch { currentpage = 1; } numresults = 0; int start = (int)((currentpage - 1) * pagesize); int to = (int)(currentpage * pagesize); if (start <= 0) start = 0; numresults = dataview.count; int a1=0; pagecount = math.divrem(numresults,pagesize,out a1); if (a1>0) { pagecount++; } if(currentpage>pagecount || currentpage<=0) { currentpage = 1; } if(currentpage==pagecount) { to = dataview.count; } // create one datatable with one column. datatable mytable = new datatable("mytable"); mytable = dataview.table.clone(); //datacolumn colitem1 = new datacolumn("name",type.gettype("system.string")); //datacolumn colitem2 = new datacolumn("types",type.gettype("system.string")); //datacolumn colitem3 = new datacolumn("vendor",type.gettype("system.string")); //mytable.columns.add(colitem1); //mytable.columns.add(colitem2); //mytable.columns.add(colitem3); //add row datarow newrow; for(int i=start;i<numresults;i++) { if(i<to) { newrow = mytable.newrow(); for(int k=0;k<dataview.table.columns.count;k++) { newrow[k] = dataview.table.rows[i][k]; } mytable.rows.add(newrow); } } mytable.acceptchanges(); dataview resultdataview = new dataview(mytable); datagrid1.datasource = resultdataview; datagrid1.databind(); /// <summary> /// 生成页导航条。 /// </summary> string strnav = ""; int endpage; if (currentpage>1) { strnav += "<a href='?page="+ (currentpage-1).tostring() +"'>上一页</a> "; } if (currentpage>11) { strnav += "<a href='?page=1'>1</a> ..."; } if(pagecount>currentpage+10) { endpage = currentpage+10; } else { endpage = pagecount; } for (int i=currentpage-10;i<endpage+1;i++) { if(i>=1) { if (i==currentpage) { strnav +="<font color=#990000><strong>"+ i.tostring() +"</strong></font> "; } else { strnav += "<a href='?page="+ i.tostring() +"'>"+ i.tostring() +"</a> "; } } } if((currentpage+10)<pagecount) { strnav += "... <a href='?page="+ pagecount.tostring() +"'>"+ pagecount.tostring() +"</a>"; } if(currentpage<pagecount) { strnav += " <a href='?page="+ (currentpage+1).tostring() +"'>下一页</a> "; } literal1.text = strnav; literal2.text = "共 "+ numresults.tostring() +" 条供应信息,当前显示第 "+ (start+1).tostring() +" - "+ to.tostring() +" 条,共 "+ pagecount.tostring() +" 页"; } #region web 窗体设计器生成的代码 override protected void oninit(eventargs e) { // // codegen: 该调用是 asp.net web 窗体设计器所必需的。 // initializecomponent(); base.oninit(e); } /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void initializecomponent() { this.load += new system.eventhandler(this.page_load); } #endregion } } |
新闻热点
疑难解答
图片精选