问题描述
前端代码:<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Gridview.aspx.cs"Inherits="gridview"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title></title></head><body><formid="form1"runat="server"><div><asp:GridViewID="GridView1"runat="server"AllowPaging="True"ShowHeaderWhenEmpty="True"AutoGenerateColumns="false"OnPageIndexChanging="GridView_test_PageIndexChanging"onselectedindexchanged="GridView1_SelectedIndexChanged"PageSize="6"><Columns><asp:TemplateFieldHeaderText="序号"><ItemTemplate><asp:Labelrunat="server"Text='<%#Eval("DATA_ID")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="水位"><ItemTemplate><asp:Labelrunat="server"Text='<%#Eval("DDZT")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="水位差"><ItemTemplate><asp:Labelrunat="server"Text='<%#Eval("DDZTD")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="测点代号"><ItemTemplate><asp:Labelrunat="server"Text='<%#Eval("DDSTCD")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="预到时间1"><ItemTemplate><asp:Labelrunat="server"Text='<%#Eval("YCSJ1")%>'></asp:Label></ItemTemplate></asp:TemplateField><asp:TemplateFieldHeaderText="预到时间2"><ItemTemplate><asp:Labelrunat="server"Text='<%#Eval("YCSJ2")%>'></asp:Label></ItemTemplate></asp:TemplateField></Columns><PagerTemplate><tablewidth="100%"><tr><tdalign="right">第<asp:LabelID="lblPageIndex"runat="server"Text='<%#((GridView)Container.Parent.Parent).PageIndex+1%>'></asp:Label>页 共<asp:LabelID="lblPageCount"runat="server"Text='<%#((GridView)Container.Parent.Parent).PageCount%>'></asp:Label>页 每页<asp:LabelID="Label1"runat="server"Text='<%#((GridView)Container.Parent.Parent).PageSize%>'></asp:Label>条 <asp:LinkButtonText="首页"ID="btnFirst"runat="server"CausesValidation="false"CommandArgument="First"CommandName="Page"/> <asp:LinkButtonText="上一页"ID="btnPre"runat="server"CausesValidation="false"CommandArgument="Prev"CommandName="Page"/> <asp:LinkButtonText="下一页"ID="btnNext"runat="server"CausesValidation="false"CommandArgument="Next"CommandName="Page"/> <asp:LinkButtonText="尾页"ID="btnLast"runat="server"CausesValidation="false"CommandArgument="Last"CommandName="Page"/></td></tr></table></PagerTemplate></asp:GridView></div></form></body></html>后台代码:usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Data;usingMySql.Data.MySqlClient;usingMySQLDriverCS;usingSystem.Configuration;publicpartialclassgridview:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){if(!Page.IsPostBack){this.InitGridView();}}privatevoidInitGridView(){StringstrSql="select*fromst_stide_ar";//查找Student表中的全部信息StringstrConn=ConfigurationManager.AppSettings["MySqlString"];//“MySqlString”为数据库编码DataSetdataSet=newDataSet();MySqlConnectionmySqlConn=newMySqlConnection(strConn);mySqlConn.Open();GridView1.PagerSettings.Mode=PagerButtons.Numeric;//GridView1.PagerSettings.NextPageText="Clickfornextpage";//GridView1.PagerSettings.PreviousPageText="Clickforpreviouspage";MySqlCommandmySqlCmd=newMySqlCommand(strSql,mySqlConn);MySqlDataAdaptermySqlDataAdapter=newMySqlDataAdapter(mySqlCmd);mySqlDataAdapter.Fill(dataSet);//填充数据集//GridView1.Columns[0].Visible=false;//隐藏一列GridView1.DataSource=dataSet.Tables[0];GridView1.DataBind();}protectedvoidGridView_test_PageIndexChanging(objectsender,GridViewPageEventArgse){GridView1.PageIndex=e.NewPageIndex;//GridView1.DataSource="**";//设置数据源GridView1.DataBind();}protectedvoidGridView1_SelectedIndexChanged(objectsender,EventArgse){}}
解决方案
解决方案二:
你既然自己写了分页,那你又不给button绑相应的处理事件……你让gridview怎么分页呢?
解决方案三:
Gridview自带的分页功能效率很差,你可以试试AspNetPager:
解决方案四:
额没分页的事件咋能分页呢?分页只写一个事件然后在里面判断CommandArgument是什么动作来分页参考