问题描述
我想做一个datagrid分页功能比如<<1234>>共4页请问如何写请高手执教先谢了
解决方案
解决方案二:
谁来救救我啊
解决方案三:
这个应该很简单的啊,可以直接编辑的吧~~你再仔细琢磨琢磨
解决方案四:
属性生成器里面设置
解决方案五:
控件上点击右键--属性生成器-分页把允许分页打上勾,页大小自己设置,模式里面选择"页码"就好了。
解决方案六:
<%@Pagelanguage="c#"EnableViewState="true"Codebehind="DataGridPaging.aspx.cs"AutoEventWireup="false"Inherits="eMeng.Exam.DataGridPaging.DataGridPaging"%><!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.0Transitional//EN"><HTML><HEAD>2e)[9`(b1W!K+O<metacontent="VisualBasic7.0"name="CODE_LANGUAGE">#<metacontent="JavaScript"name="vs_defaultClientScript"><metacontent="http://schemas.microsoft.com/intellisense/ie5"name="vs_targetSchema"></HEAD><bodyMS_POSITIONING="GridLayout"><formid="Form1"runat="server"><asp:datagridid="MyDataGrid"runat="server"AutoGenerateColumns="False"HorizontalAlign="Center"AlternatingItemStyle-BackColor="#eeeeee"7HeaderStyle-BackColor="#aaaadd"Font-Size="8pt"Font-Name="Verdana"CellSpacing="0"CellPadding="3"GridLines="Both"BorderWidth="1"#BorderColor="black"OnPageIndexChanged="MyDataGrid_Page"PagerStyle-HorizontalAlign="Right"PagerStyle-Mode="NumericPages"PageSize="5"AllowPaging="True"><AlternatingItemStyleBackColor="#EEEEEE"></AlternatingItemStyle><HeaderStyleBackColor="#AAAADD"Font-Bold="True"HorizontalAlign="Center"></HeaderStyle><PagerStyleHorizontalAlign="Right"Mode="NumericPages"></PagerStyle><Columns><asp:BoundColumnHeaderText="标题"DataField="Title"HeaderStyle-Width="480px"></asp:BoundColumn><asp:BoundColumnHeaderText="发表日期"DataField="CreateDate"DataFormatString="{0:yyyy-MM-ddhh:mm:ss}"></asp:BoundColumn></Columns></asp:datagrid><pstyle="FONT-SIZE:9pt"align="center">2<asp:labelid="lblPageCount"runat="server"></asp:label><asp:labelid="lblCurrentIndex"runat="server"></asp:label><asp:linkbuttonid="btnFirst"onclick="PagerButtonClick"runat="server"Font-Name="verdana"Font-size="8pt"ForeColor="navy"CommandArgument="0"></asp:linkbutton><asp:linkbuttonid="btnPrev"onclick="PagerButtonClick"runat="server"Font-Name="verdana"Font-size="8pt"ForeColor="navy"CommandArgument="prev"></asp:linkbutton><asp:linkbuttonid="btnNext"onclick="PagerButtonClick"runat="server"Font-Name="verdana"Font-size="8pt"ForeColor="navy"CommandArgument="next"></asp:linkbutton><asp:linkbuttonid="btnLast"onclick="PagerButtonClick"runat="server"Font-Name="verdana"Font-size="8pt"ForeColor="navy"CommandArgument="last"></asp:linkbutton></p></form></body></HTML>DataGridPaging.aspxusingSystem;usingSystem.Collections;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Data.OleDb;usingSystem.Drawing;usingSystem.Web;usingSystem.Web.SessionState;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Web.UI.HtmlControls;namespaceeMeng.Exam.DataGridPaging{///<summary>///DataGridPaging的摘要说明///</summary>publicclassDataGridPaging:System.Web.UI.Page{protectedSystem.Web.UI.WebControls.DataGridMyDataGrid;protectedSystem.Web.UI.WebControls.LabellblPageCount;protectedSystem.Web.UI.WebControls.LabellblCurrentIndex;protectedSystem.Web.UI.WebControls.LinkButtonbtnFirst;protectedSystem.Web.UI.WebControls.LinkButtonbtnPrev;protectedSystem.Web.UI.WebControls.LinkButtonbtnNext;protectedSystem.Web.UI.WebControls.LinkButtonbtnLast;privateOleDbConnectioncn=newOleDbConnection();privatevoidPage_Load(objectsender,System.EventArgse){//在此处放置用户代码以初始化页面:btnFirst.Text="最首页";btnPrev.Text="前一页";btnNext.Text="下一页";btnLast.Text="最后页";OpenDatabase();BindGrid();}privatevoidOpenDatabase(){cn.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+Server.MapPath("xxxx.mdb");cn.Open();}privatevoidShowStats(){lblCurrentIndex.Text="第"+(MyDataGrid.CurrentPageIndex+1).ToString()+"页";lblPageCount.Text="总共"+MyDataGrid.PageCount.ToString()+"页";}publicvoidPagerButtonClick(objectsender,EventArgse){stringarg=((LinkButton)sender).CommandArgument.ToString()switch(arg)case"next":if(MyDataGrid.CurrentPageIndex<(MyDataGrid.PageCount-1)){MyDataGrid.CurrentPageIndex+=1;}break;case"prev":if(MyDataGrid.CurrentPageIndex>0){MyDataGrid.CurrentPageIndex-=1;}break;case"last":MyDataGrid.CurrentPageIndex=(MyDataGrid.PageCount-1);break;default:MyDataGrid.CurrentPageIndex=System.Convert.ToInt32(arg);break;}BindGrid();ShowStats();}publicvoidBindGrid()#m$S.M_0I&e'c0{{OleDbConnectionmyConnection=cn;DataSetds=newDataSet();OleDbDataAdapteradapter=newOleDbDataAdapter("SelectTitle,CreateDatefromDocument",myConnection);adapter.Fill(ds,"Document");MyDataGrid.DataSource=ds.Tables["Document"].DefaultView;MyDataGrid.DataBind();ShowStats();}publicvoidMyDataGrid_Page(objectsender,DataGridPageChangedEventArgse)*i+R$];{intstartIndex;startIndex=MyDataGrid.CurrentPageIndex*MyDataGrid.PageSize;:;MyDataGrid.CurrentPageIndex=e.NewPageIndex;BindGrid();ShowStats();}#regionWebFormDesignergeneratedcodeoverrideprotectedvoidOnInit(EventArgse){////CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。//InitializeComponent();base.OnInit(e);}///<summary>"///设计器支持所需的方法-不要使用代码编辑器修改///此方法的内容。///</summary>)privatevoidInitializeComponent()){this.Load+=newSystem.EventHandler(this.Page_Load);}#endregion}}
解决方案七:
为什么不用系统自己带的分页呢?建议你找相关例子来看~下面是一个自己分页的例子给repeater的,修改下可以用privatevoidNewsBind()//repeater分页并绑定{//stringSqlStr="SELECT[ThesisID],[ThesisName],[ThesisCorp],[ThesisOwner]FROM[ConferThesis]WHERE([ConferID]=@ConferID)ORDERBY[ThesisCorp]";stringSqlStr="select[ConferID],[ConferName]from[ConferInfo]WHERE[TagData]='V'orderby[ConferID]";stringconnectionString=System.Configuration.ConfigurationManager.ConnectionStrings["ChemInterConn"].ToString();SqlConnectionconn=newSqlConnection(connectionString);conn.Open();SqlDataAdapterAdapter=newSqlDataAdapter(SqlStr,conn);DataSetds=newDataSet();try{Adapter.Fill(ds,"testTable");PagedDataSourceobjPage=newPagedDataSource();objPage.DataSource=ds.Tables["testTable"].DefaultView;objPage.AllowPaging=true;objPage.PageSize=5;intCurPage;if(Request.QueryString["Page"]!=null){CurPage=Convert.ToInt32(Request.QueryString["page"]);}else{CurPage=1;}objPage.CurrentPageIndex=CurPage-1;LeaveMessage.DataSource=objPage;//这里更改控件名称LeaveMessage.DataBind();//这里更改控件名称RecordCount.Text=objPage.DataSourceCount.ToString();PageCount.Text=objPage.PageCount.ToString();Pageindex.Text=CurPage.ToString();Literal1.Text=PageList(objPage.PageCount,CurPage);//Literal1.Text=PageList(objPage.PageCount,Pageindex,L_Manage);//带参数的:LManage为参数FirstPage.NavigateUrl=Request.CurrentExecutionFilePath+"?page=1";PrevPage.NavigateUrl=Request.CurrentExecutionFilePath+"?page="+(CurPage-1);NextPage.NavigateUrl=Request.CurrentExecutionFilePath+"?page="+(CurPage+1);LastPaeg.NavigateUrl=Request.CurrentExecutionFilePath+"?page="+objPage.PageCount.ToString();if(CurPage<=1&&objPage.PageCount<=1){FirstPage.NavigateUrl="";PrevPage.NavigateUrl="";NextPage.NavigateUrl="";LastPaeg.NavigateUrl="";/**//**//**//*FirstPage.Visible=false;PrevPage.Visible=false;NextPage.Visible=false;LastPaeg.Visible=false;*/}if(CurPage<=1&&objPage.PageCount>1){FirstPage.NavigateUrl="";PrevPage.NavigateUrl="";/**//**//**//*FirstPage.Visible=false;PrevPage.Visible=false;*/}if(CurPage>=objPage.PageCount){NextPage.NavigateUrl="";LastPaeg.NavigateUrl="";/**//**//**//*NextPage.Visible=false;LastPaeg.Visible=false;*/}}catch(Exceptionerror){Response.Write(error.ToString());}finally{conn.Close();}}privatestringPageList(intPagecount,intPageindex)//privatestringJump_List(intPagecount,intPageindex,longL_Manage)//带参数的传递{StringBuildersb=newStringBuilder();//下为带参数的传递//sb.Append("<selectid="Page_Jump"name="Page_Jump"onchange="window.location='"+Request.CurrentExecutionFilePath+"?page='+this.options[this.selectedIndex].value+'&Org_ID="+L_Manage+"';">");//不带参数的传递sb.Append("<selectid="Page_Jump"name="Page_Jump"onchange="window.location='"+Request.CurrentExecutionFilePath+"?page='+this.options[this.selectedIndex].value+'';">");for(inti=1;i<=Pagecount;i++){if(Pageindex==i)sb.Append("<optionvalue='"+i+"'selected>"+i+"</option>");elsesb.Append("<optionvalue='"+i+"'>"+i+"</option>");}sb.Append("</select>");returnsb.ToString();}
解决方案八:
数据量大就用存储过程分页,邹杰写过
解决方案九:
这个在属性生成器里面有哦!我用过哦!很好用!慢慢琢磨吧!