|
由于工作的需要,为了提高以后的开发效率,故创建了这样的一个控件。 效果如图: ![]() 控件代码如下: using System;
using System.Collections.Generic; using System.ComponentModel; using System.Text; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using System.Web.UI.HtmlControls; namespace Jec.CustomControls { [DefaultProperty("Text")] [ToolboxData("<{0}:JAGridView runat=server></{0}:JAGridView>")] public class JAGridView : Anthem.GridView, INamingContainer { //翻页按钮 private Anthem.LinkButton lbtnFirst; private Anthem.LinkButton lbtnPrev; private Anthem.LinkButton lbtnNext; private Anthem.LinkButton lbtnLast; //当前页数 总页数 每页记录数 总记录数 private Anthem.Label lbPageIndex; private Anthem.Label lbPageCount; private Anthem.Label lbPageSize; private Anthem.Label lbRecordCount; //记录总数 private int recordCount; //初始化GridView参数 protected override void OnLoad(EventArgs e) { this.AllowPaging = true; this.PagerSettings.Visible = true; base.OnLoad(e); } protected override void InitializePager(GridViewRow row, int columnSpan, PagedDataSource pagedDataSource) { //获取记录总数 recordCount = pagedDataSource.DataSourceCount; //初始化 导航按钮 lbtnFirst = new Anthem.LinkButton(); lbtnFirst.ID = "lbtnFirst"; lbtnFirst.Text = "首页"; lbtnFirst.CommandName = "First"; lbtnFirst.Command += new CommandEventHandler(lbtn_Command); lbtnPrev = new Anthem.LinkButton(); lbtnPrev.ID = "lbtnPrev"; lbtnPrev.Text = "上一页"; lbtnPrev.CommandName = "Prev"; lbtnPrev.Command += new CommandEventHandler(lbtn_Command); lbtnNext = new Anthem.LinkButton(); lbtnNext.ID = "lbtnNext"; lbtnNext.Text = "下一页"; lbtnNext.CommandName = "Next"; lbtnNext.Command += new CommandEventHandler(lbtn_Command); lbtnLast = new Anthem.LinkButton(); lbtnLast.ID = "lbtnLast"; lbtnLast.Text = "尾页"; lbtnLast.CommandName = "Last"; lbtnLast.Command += new CommandEventHandler(lbtn_Command); //下拉跳转列表 Anthem.DropDownList ddl = new Anthem.DropDownList(); for (int i = 0; i < PageCount; i++) { ddl.Items.Add(new ListItem(Convert.ToString(i + 1), i.ToString())); } ddl.EnableCallBack = true; ddl.AutoCallBack = true; ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged); ddl.SelectedIndex = PageIndex; //初始化 状态列表 lbPageIndex = new Anthem.Label(); lbPageIndex.ID = "lbPageIndex"; lbPageCount = new Anthem.Label(); lbPageCount.ID = "lbPageCount"; lbPageSize = new Anthem.Label(); lbPageSize.ID = "lbPageSize"; lbRecordCount = new Anthem.Label(); lbRecordCount.ID = "lbRecordCount"; Table tbl = new Table(); tbl.BorderWidth = 0; tbl.Width = Unit.Percentage(100); tbl.Rows.Add(new TableRow()); TableCell cell_1 = new TableCell(); cell_1.Controls.Add(lbtnFirst); cell_1.Controls.Add(new LiteralControl(" ")); cell_1.Controls.Add(lbtnPrev); cell_1.Controls.Add(new LiteralControl(" ")); cell_1.Controls.Add(lbtnNext); cell_1.Controls.Add(new LiteralControl(" ")); cell_1.Controls.Add(lbtnLast); cell_1.Controls.Add(new LiteralControl(" ")); TableCell cell_2 = new TableCell(); cell_2.Controls.Add(ddl); cell_2.Controls.Add(new LiteralControl(" 当前页数:")); cell_2.Controls.Add(lbPageIndex); cell_2.Controls.Add(new LiteralControl(" 总页数:")); cell_2.Controls.Add(lbPageCount); cell_2.Controls.Add(new LiteralControl(" 每页记录数:")); cell_2.Controls.Add(lbPageSize); cell_2.Controls.Add(new LiteralControl(" 总记录数:")); cell_2.Controls.Add(lbRecordCount); tbl.Rows[0].Cells.Add(cell_1); tbl.Rows[0].Cells.Add(cell_2); tbl.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Left; tbl.Rows[0].Cells[1].HorizontalAlign = HorizontalAlign.Right; //将分页按钮和状态栏添加到 GridView的分页行 row.Controls.AddAt(0, new TableCell()); row.Cells[0].ColumnSpan = Columns.Count; row.Cells[0].Controls.AddAt(0, tbl); SetPagerStatus(); } //改变下拉菜单 触发翻页 protected void ddl_SelectedIndexChanged(object sender, EventArgs e) { PageIndex = ((DropDownList)sender).SelectedIndex; } //点击按钮时 进行翻页 void lbtn_Command(object sender, CommandEventArgs e) { switch (e.CommandName) { case "First": { PageIndex = 0; break; } case "Prev": { if (PageIndex > 0) { PageIndex--; } break; } case "Next": { if (PageIndex + 1 < PageCount) { PageIndex++; } break; } case "Last": { PageIndex = PageCount - 1; break; } } } //设置分页状态 public void SetPagerStatus() { //如果没记录则不显示 if (PageCount == 0) { this.Visible = false; return; } //第一页 if (PageIndex == 0) { lbtnFirst.Enabled = false; lbtnPrev.Enabled = false; lbtnNext.Enabled = true; lbtnLast.Enabled = true; } //最后一页 else if (PageIndex == PageCount - 1) { lbtnFirst.Enabled = true; lbtnPrev.Enabled = true; lbtnNext.Enabled = false; lbtnLast.Enabled = false; } //中间页 else { lbtnFirst.Enabled = true; lbtnPrev.Enabled = true; lbtnNext.Enabled = true; lbtnLast.Enabled = true; } lbPageCount.Text = PageCount.ToString(); lbRecordCount.Text = recordCount.ToString(); lbPageIndex.Text = (PageIndex + 1).ToString(); lbPageSize.Text = PageSize.ToString(); lbtnFirst.UpdateAfterCallBack = true; lbtnPrev.UpdateAfterCallBack = true; lbtnNext.UpdateAfterCallBack = true; lbtnLast.UpdateAfterCallBack = true; lbPageIndex.UpdateAfterCallBack = true; lbPageCount.UpdateAfterCallBack = true; lbPageSize.UpdateAfterCallBack = true; lbRecordCount.UpdateAfterCallBack = true; } } } 在aspx页面使用自定义控件,和GridView的使用方法几乎一样。DataSourceID设置为数据源的ID.
<cc1:JAGridView ID="JAGridView1" runat="server" AllowPaging="True" DataSourceID="ObjectDataSource1"
AutoGenerateColumns="false"> <Columns> <asp:BoundField HeaderText="产品编号" DataField="ProductID" /> <asp:BoundField HeaderText="产品名称" DataField="ProductName" /> <asp:BoundField HeaderText="价格" DataField="UnitPrice" /> <asp:BoundField HeaderText="单位数量" DataField="QuantityPerUnit" /> </Columns> <PagerStyle BackColor="#C0C0FF" ForeColor="ControlText" CssClass="Pager" /> </cc1:JAGridView> 然后创建ObjectDataSource数据源
asp:ObjectDataSource ID="ObjectDataSource1" EnablePaging="true" TypeName="Test.Controls.App_Data.DataAccess"
SelectCountMethod="GetPageCount" SelectMethod="GetCustomList" StartRowIndexParameterName="startRowIndex" MaximumRowsParameterName="maximumRows" runat="server"></asp:ObjectDataSource>注意里面的几个参数 TypeName是业务逻辑层的名字空间+类名,SelectCountMethod需要你在业务逻辑中实现返回记录数的函数。SelectMethod需要在业务逻辑中实现选择分页相应记录的函数,该函数有startRowIndex和maximumRows两个参数,分别代码起始行数,返回的记录数。
最后创建业务逻辑和数据访问层如下: using System;
using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Data.SqlClient;![]() namespace Test.Controls.App_Data{ public class DataAccess { public int GetPageCount() { using (SqlConnection conn = new SqlConnection("Data Source=(local); User ID=sa;Password=sa;Initial Catalog=Northwind")) {
conn.Open(); SqlCommand cmd = new SqlCommand("select count(ProductID) from Products", conn); return (int)cmd.ExecuteScalar(); } } public DataSet GetCustomList(int startRowIndex, int maximumRows) { using (SqlConnection conn = new SqlConnection("Data Source=(local); User ID=sa;Password=sa;Initial Catalog=Northwind")) { DataSet ds = new DataSet(); try { conn.Open(); string strSql = "SELECT * FROM (SELECT ROW_NUMBER() OVER (ORDER BY ProductID) AS RowNumber, * FROM Products) Results WHERE RowNumber between " + (startRowIndex + 1) + " and " + (startRowIndex + maximumRows); SqlDataAdapter da = new SqlDataAdapter(strSql, conn); ds = new DataSet(); da.Fill(ds); } catch (Exception e) { string a = e.ToString(); } return ds; } } } } (责任编辑:admin) |

AutoGenerateColumns




骆驼户外男 真皮磨砂日常休闲鞋 低帮 2011秋冬新款 专柜正品特价