问题描述
本人属于菜鸟,跪求GridView利用存储过程分页的代码,最好有详细的说明。使用的后台语言为C#,数据库为SQLServer。不胜感谢!
解决方案
解决方案二:
可以用session也可以用隐藏控件之类的
解决方案三:
俄搞错了我还以为是状态保存
解决方案四:
<asp:LabelID="lblPage"runat="server"Text='<%#"第"+(((GridView)Container.NamingContainer).PageIndex+1)+"页/共"+(((GridView)Container.NamingContainer).PageCount)+"页"%>'></asp:Label><asp:LinkButtonID="lbnFirst"runat="Server"Text="首页"Enabled='<%#((GridView)Container.NamingContainer).PageIndex!=0%>'CommandName="Page"CommandArgument="First"></asp:LinkButton><asp:LinkButtonID="lbnPrev"runat="server"Text="上一页"Enabled='<%#((GridView)Container.NamingContainer).PageIndex!=0%>'CommandName="Page"CommandArgument="Prev"></asp:LinkButton><asp:LinkButtonID="lbnNext"runat="Server"Text="下一页"Enabled='<%#((GridView)Container.NamingContainer).PageIndex!=(((GridView)Container.NamingContainer).PageCount-1)%>'CommandName="Page"CommandArgument="Next"></asp:LinkButton><asp:LinkButtonID="lbnLast"runat="Server"Text="尾页"Enabled='<%#((GridView)Container.NamingContainer).PageIndex!=(((GridView)Container.NamingContainer).PageCount-1)%>'CommandName="Page"CommandArgument="Last"></asp:LinkButton>到第<asp:TextBoxrunat="server"ID="inPageNum"Width="100px"></asp:TextBox>页<asp:ButtonID="Button1"CommandName="go"runat="server"Text="go"/>
#regiongridView索引改变时触发事件protectedvoidGridView_PageIndexChanging(objectsender,GridViewPageEventArgse){try{GridView.PageIndex=e.NewPageIndex;bind();TextBoxtb=(TextBox)GridView.BottomPagerRow.FindControl("inPageNum");tb.Text=(GridView.PageIndex+1).ToString();}catch{}}#endregion#regiongridView生成事件是触发protectedvoidGridView_RowCommand(objectsender,GridViewCommandEventArgse){if(e.CommandName=="go"){try{TextBoxtb=(TextBox)GridView.BottomPagerRow.FindControl("inPageNum");intnum=Int32.Parse(tb.Text);GridViewPageEventArgsea=newGridViewPageEventArgs(num-1);GridView_PageIndexChanging(null,ea);}catch{}}}#endregion
解决方案五:
可以用AspNetPager配合分页存储过程实现:
解决方案六:
<asp:GridViewID="GridView1"runat="server"AutoGenerateColumns="False"Width="100%"onrowdatabound="GridView1_RowDataBound"PageSize="15"onrowdeleting="GridView1_RowDeleting"onrowediting="GridView1_RowEditing"onpageindexchanging="GridView1_PageIndexChanging"AllowPaging="True"EnableModelValidation="True"><Columns>//你的行的显示代码</Columns><pagertemplate><tablewidth="100%"class="pager"><tr><tdstyle="text-align:right">第<asp:Labelid="lblPageIndex"runat="server"text='<%#((GridView)Container.Parent.Parent).PageIndex+1%>'/>页共<asp:Labelid="lblPageCount"runat="server"text='<%#((GridView)Container.Parent.Parent).PageCount%>'/>页<asp:linkbuttonid="btnFirst"runat="server"causesvalidation="False"commandargument="First"commandname="Page"text="首页"/><asp:linkbuttonid="btnPrev"runat="server"causesvalidation="False"commandargument="Prev"commandname="Page"text="上一页"/><asp:linkbuttonid="btnNext"runat="server"causesvalidation="False"commandargument="Next"commandname="Page"text="下一页"/><asp:linkbuttonid="btnLast"runat="server"causesvalidation="False"commandargument="Last"commandname="Page"text="尾页"/><asp:textboxid="txtNewPageIndex"runat="server"width="20px"text='<%#((GridView)Container.Parent.Parent).PageIndex+1%>'/><asp:linkbuttonid="btnGo"runat="server"causesvalidation="False"commandargument="-1"commandname="Page"text="GO"/><!--heresettheCommandArgumentoftheGoButtonto'-1'astheflag--></td></tr></table></pagertemplate></asp:GridView>
protectedvoidGridView1_PageIndexChanging(objectsender,GridViewPageEventArgse){GridViewtheGrid=senderasGridView;//refertotheGridViewintnewPageIndex=0;if(-2==e.NewPageIndex){//whenclickthe"GO"ButtonTextBoxtxtNewPageIndex=null;//GridViewRowpagerRow=theGrid.Controls[0].Controls[theGrid.Controls[0].Controls.Count-1]asGridViewRow;//refertoPagerTemplateGridViewRowpagerRow=theGrid.BottomPagerRow;//GridView较DataGrid提供了更多的API,获取分页块可以使用BottomPagerRow或者TopPagerRow,当然还增加了HeaderRow和FooterRow//updatedat2006年6月21日3:15:33if(null!=pagerRow){txtNewPageIndex=pagerRow.FindControl("txtNewPageIndex")asTextBox;//refertotheTextBoxwiththeNewPageIndexvalue}if(null!=txtNewPageIndex){newPageIndex=int.Parse(txtNewPageIndex.Text)-1;//gettheNewPageIndex}}else{//whenclickthefirst,last,previousandnextButtonnewPageIndex=e.NewPageIndex;}//checktopreventformtheNewPageIndexoutoftherangenewPageIndex=newPageIndex<0?0:newPageIndex;newPageIndex=newPageIndex>=theGrid.PageCount?theGrid.PageCount-1:newPageIndex;//specifytheNewPageIndextheGrid.PageIndex=newPageIndex;//控件相应的绑定代码//rebindthecontrol//inthiscaseofretrievingthedatausingthexxxDataSoucrcontrol,//justdonothing,becausetheasp.netenginebindsthedataautomatically}
以上这两个就ok了还有就是也可以用楼上大哥的分页控件,当然LZ也可以参考下这个分页