效果图:
前端页面
<form id="form1" runat="server"> <div> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"> <Columns> <asp:BoundField DataField="Typeid" HeaderText="编号" /> <asp:BoundField DataField="TypeName" HeaderText="类型名称" /> <asp:BoundField DataField="Upid" HeaderText="父Id" /> <asp:BoundField DataField="Memo" HeaderText="备注" /> </Columns> <PagerTemplate> <table style="font:11px Tahoma; background-color:#cfe3fb;"> <tr> <td style="text-align: right;"> 第<asp:Label ID="lblPageIndex" runat="server" Text=‘<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>‘></asp:Label>页 / 共<asp:Label ID="lblPageCount" runat="server" Text=‘<%# ((GridView)Container.Parent.Parent).PageCount %>‘></asp:Label>页 <asp:LinkButton ID="btnFirst" runat="server" CausesValidation="False" CommandName="Page" Text="首页" CommandArgument="first" OnClick="btnFirst_Click"> </asp:LinkButton> <asp:LinkButton ID="btnPrev" runat="server" CausesValidation="False" CommandName="Page" Text="上一页" CommandArgument="prev" onclick="btnFirst_Click"> </asp:LinkButton> <asp:LinkButton ID="btnNext" runat="server" CausesValidation="False" CommandName="Page" Text="下一页" CommandArgument="next" OnClick="btnFirst_Click"> </asp:LinkButton> <asp:LinkButton ID="btnLast" runat="server" CausesValidation="False" CommandName="Page" Text="尾页" CommandArgument="last" OnClick="btnFirst_Click"> </asp:LinkButton> <asp:TextBox ID="txtNewPageIndex" runat="server" MaxLength="5" CssClass="inputBorder1" Text=‘<%# ((GridView)Container.Parent.Parent).PageIndex + 1 %>‘ onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,‘‘)}else{this.value=this.value.replace(/\D/g,‘‘)}" onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,‘‘)}else{this.value=this.value.replace(/\D/g,‘‘)}" AutoPostBack="True" ></asp:TextBox> <asp:LinkButton ID="btnGo" runat="server" CausesValidation="False" CommandArgument="go" CommandName="Page" Text="GO" OnClick="btnFirst_Click"></asp:LinkButton> </td> </tr> </table> </PagerTemplate> </asp:GridView> </div> </form>
后台代码
public partial class Default : System.Web.UI.Page { SqlConnection connect; SqlCommand cmd; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void btnFirst_Click(object sender, EventArgs e) { string str = ((LinkButton)sender).CommandArgument.ToString(); if (str != null) { if (!(str == "first")) { if (str == "last") { this.GridView1.PageIndex = this.GridView1.PageCount - 1; } else if (str == "prev") { if (this.GridView1.PageIndex >= 1) { this.GridView1.PageIndex--; } else { this.GridView1.PageIndex = 0; } } else if (str == "next") { this.GridView1.PageIndex++; } else if (str == "go") { TextBox box = (TextBox)this.GridView1.BottomPagerRow.FindControl("txtNewPageIndex"); int num = Convert.ToInt32(box.Text.ToString()); this.GridView1.PageIndex = num - 1; } } else { this.GridView1.PageIndex = 0; } } this.bind(); } /// <summary> /// 绑定数据 /// </summary> /// <returns></returns> public void bind() { connect = new SqlConnection("server=.;database=SuperMarket;uid=sa;pwd=123"); cmd = new SqlCommand(); string sql = "select * from dbo.GoodsTypes"; cmd.CommandText = sql; cmd.Connection = connect; connect.Open(); SqlDataAdapter da = new SqlDataAdapter(cmd); DataSet ds = new DataSet(); da.Fill(ds); GridView1.DataKeyNames = new string[] { "typeid" };//主键 GridView1.DataSource = ds; GridView1.DataBind(); } }
ps: 点击 go 之后为什么不跳转第二页,页面刷新了一下,记住加载事件里要加
if (!IsPostBack) { bind(); }
时间: 2024-11-06 17:52:02