HTML页面
<%@ Register assembly="AspNetPager" namespace="Wuqi.Webdiyer" tagprefix="webdiyer" %> <asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server"> <div class="jumbotron"> <asp:GridView ID="GridView1" runat="server" AllowSorting="true" OnSorting="GridView1_Sorting" Width="98%" OnRowCommand="GridView1_RowCommand" AutoGenerateColumns="false"> <RowStyle Height="20px" HorizontalAlign="Center" ForeColor="#000066"></RowStyle> <Columns> <asp:BoundField DataField="ID" HeaderText="编号" SortExpression="ID"/> <asp:BoundField DataField="HotelID" HeaderText="酒店编码" SortExpression="HotelID"/> <asp:BoundField DataField="TypeName" HeaderText="类型名称" SortExpression="TypeName"/> <asp:BoundField DataField="CreateTime" HeaderText="生成时间" SortExpression="CreateTime"/> <asp:BoundField DataField="CreateBy" HeaderText="创建人" SortExpression="CreateBy"/> <asp:TemplateField HeaderText="操作"> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Del" CommandArgument='<%# Eval("ID") %>'>删除</asp:LinkButton> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> <div> <webdiyer:aspnetpager ID="AspNetPager1" runat="server" PageSize="35" Width="95%" FirstPageText="首页" PrevPageText="前页" LayoutType="Table" NextPageText="后页" LastPageText="尾页" AlwaysShow="true" UrlPaging="false" showcustominfosection="Left" onpagechanging="AspNetPager1_PageChanging" class="gl-number-page"> </webdiyer:aspnetpager> </div> </asp:Content>
Model CardType类
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Web; [Serializable] [SugarTable("tb_CardType")] public class CardType { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int ID { get; set; } public int HotelID { get; set; } public string TypeName { get; set; } public DateTime CreateTime { get; set; } public string CreateBy { get; set; } }
页面后台代码
using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; public partial class _Default : Page { protected SqlSugarClient db = new SqlSugarClient(new ConnectionConfig() { ConnectionString = "server=JYJKQC53O;uid=sa;pwd=1a2s3d4f5g6h;database=Hotel345", DbType = DbType.SqlServer,//设置数据库类型 IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放 InitKeyType = InitKeyType.Attribute //从实体特性中读取主键自增列信息 }); protected void Page_Load(object sender, EventArgs e) { if (db.Queryable<CardType>().Count() < 50) { var list = new List<CardType>(); CardType cartype; for (int i = 0; i < 50; i++) { cartype = new CardType(); cartype.HotelID = 3; cartype.TypeName = "居民身份证" + i.ToString(); cartype.CreateTime = DateTime.Now; cartype.CreateBy = "fermnng"; list.Add(cartype); } db.Insertable(list.ToArray()).ExecuteCommand(); } if (!Page.IsPostBack) { ViewState["sort"] = "ASC"; ViewState["sortExpression"] = "ID"; DataBin(); } } protected void DataBin() { int total = 0; var getPage = db.Queryable<CardType>().ToPageList(AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, ref total);//根据分页查询 if (ViewState["sort"].ToString() == "ASC") { getPage = getPage.OrderBy(s => GetPropertyValue(s, ViewState["sortExpression"].ToString())).ToList<CardType>(); } else { getPage = getPage.OrderByDescending(s => GetPropertyValue(s, ViewState["sortExpression"].ToString())).ToList<CardType>(); } this.GridView1.DataSource = getPage; this.GridView1.DataBind(); AspNetPager1.RecordCount = total; ViewState["getPage"] = getPage; } protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e) { AspNetPager1.CurrentPageIndex = e.NewPageIndex; DataBin(); } protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { string sortExpression = e.SortExpression; var list = ViewState["getPage"] as List<CardType>; if (ViewState["sort"].ToString() == "ASC") { list = list.OrderByDescending(s => GetPropertyValue(s, sortExpression)).ToList<CardType>(); ViewState["sort"] = "DESC"; } else { list = list.OrderBy(s => GetPropertyValue(s, sortExpression)).ToList<CardType>(); ViewState["sort"] = "ASC"; } if (list.Count > 0) { ViewState["sortExpression"] = sortExpression; GridView1.DataSource = list; GridView1.DataBind(); } } public object GetPropertyValue(object obj, string property) { System.Reflection.PropertyInfo propertyInfo = obj.GetType().GetProperty(property); return propertyInfo.GetValue(obj, null); } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Del") { int RowID = Convert.ToInt32(e.CommandArgument); var result = db.Deleteable<CardType>().In(RowID).ExecuteCommand(); DataBin(); } } }