// 翻页
private void dgdList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dgCommon.PageChange(dgdList, e);
}
// 排序
private void dgdList_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
this.dgCommon.SortDataGrid(dgdList, e);
}
// 设置排序图标
private void dgdList_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
this.dgCommon.SetItemCreated(dgdList, e);
}
private void dgdList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dgCommon.PageChange(dgdList, e);
}
// 排序
private void dgdList_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
this.dgCommon.SortDataGrid(dgdList, e);
}
// 设置排序图标
private void dgdList_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
this.dgCommon.SetItemCreated(dgdList, e);
}
接下来看如何实现:
DataGrid公用操作类,提供排序,翻页,添加数据源,设置排序图标等功能。
using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
namespace NameSpace
{
/// <summary>
/// 控件DataGrid的通用类。
/// </summary>
/// <remarks>该类中包括对DataGrid的排序,添加数据源等方法。
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
/// </remarks>
public class DataGridCommon
{
声明委托
声明事件
/// <summary>
/// 构造函数。
/// </summary>
/// <param name="viewStateBag">ViewState</param>
/// <example>案例代码:
/// <code>
/// DataGridCommon dataGridCommon = new DataGridCommon(ViewState);
/// </code>
/// </example>
/// <remarks>
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
/// </remarks>
public DataGridCommon(SetDataGridSourceHandler onBindEvent, SetDataGridSortHandler sortEvent, SetDataGridItemCreateHandler onItemCreateEvent)
{
bindDataGrid = onBindEvent;
sortDataGrid = sortEvent;
itemCreateDataGrid = onItemCreateEvent;
}
添加DataGrid的数据源
/// <summary>
/// 取得排序字段。
/// </summary>
/// <param name="e"></param>
public void SetItemCreated(DataGrid dgdList, DataGridItemEventArgs e)
{
if (itemCreateDataGrid != null)
{
itemCreateDataGrid(dgdList, e);
}
}
/// <summary>
/// 排序。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void SortDataGrid(DataGrid dgdList, DataGridSortCommandEventArgs e)
{
if (sortDataGrid != null)
{
sortDataGrid(dgdList, e);
}
}
/// <summary>
/// 分页。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void PageChange(DataGrid dgdList, DataGridPageChangedEventArgs e)
{
dgdList.EditItemIndex = -1;
dgdList.SelectedIndex = -1;
dgdList.CurrentPageIndex = e.NewPageIndex;
BindDataGrid(dgdList);
}
/// <summary>
/// 取消编辑。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void EditCancel(DataGrid dgdList, DataGridCommandEventArgs e)
{
dgdList.EditItemIndex = -1;
BindDataGrid(dgdList);
}
/// <summary>
/// 编辑。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void EditDataGrid(DataGrid dgdList, DataGridCommandEventArgs e)
{
dgdList.EditItemIndex = e.Item.ItemIndex;
dgdList.SelectedIndex = -1;
BindDataGrid(dgdList);
}
取得相关值域的TableCell
取得相关值域的HyperLink
}
}
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.UI;
namespace NameSpace
{
/// <summary>
/// 控件DataGrid的通用类。
/// </summary>
/// <remarks>该类中包括对DataGrid的排序,添加数据源等方法。
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
/// </remarks>
public class DataGridCommon
{
声明委托
声明事件
/// <summary>
/// 构造函数。
/// </summary>
/// <param name="viewStateBag">ViewState</param>
/// <example>案例代码:
/// <code>
/// DataGridCommon dataGridCommon = new DataGridCommon(ViewState);
/// </code>
/// </example>
/// <remarks>
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
/// </remarks>
public DataGridCommon(SetDataGridSourceHandler onBindEvent, SetDataGridSortHandler sortEvent, SetDataGridItemCreateHandler onItemCreateEvent)
{
bindDataGrid = onBindEvent;
sortDataGrid = sortEvent;
itemCreateDataGrid = onItemCreateEvent;
}
添加DataGrid的数据源
/// <summary>
/// 取得排序字段。
/// </summary>
/// <param name="e"></param>
public void SetItemCreated(DataGrid dgdList, DataGridItemEventArgs e)
{
if (itemCreateDataGrid != null)
{
itemCreateDataGrid(dgdList, e);
}
}
/// <summary>
/// 排序。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void SortDataGrid(DataGrid dgdList, DataGridSortCommandEventArgs e)
{
if (sortDataGrid != null)
{
sortDataGrid(dgdList, e);
}
}
/// <summary>
/// 分页。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void PageChange(DataGrid dgdList, DataGridPageChangedEventArgs e)
{
dgdList.EditItemIndex = -1;
dgdList.SelectedIndex = -1;
dgdList.CurrentPageIndex = e.NewPageIndex;
BindDataGrid(dgdList);
}
/// <summary>
/// 取消编辑。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void EditCancel(DataGrid dgdList, DataGridCommandEventArgs e)
{
dgdList.EditItemIndex = -1;
BindDataGrid(dgdList);
}
/// <summary>
/// 编辑。
/// </summary>
/// <param name="dgdList"></param>
/// <param name="e"></param>
public void EditDataGrid(DataGrid dgdList, DataGridCommandEventArgs e)
{
dgdList.EditItemIndex = e.Item.ItemIndex;
dgdList.SelectedIndex = -1;
BindDataGrid(dgdList);
}
取得相关值域的TableCell
取得相关值域的HyperLink
}
}
结合页面基类使用:
using System;
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Web表现层类的基类。
/// </summary>
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
public class BasePage : System.Web.UI.Page
{
/// <summary>
/// 处理DataGrid的对象。
/// </summary>
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
protected DataGridCommon dgCommon = null;
public BasePage() : base()
{
// 注册处理DataGrid对象的方法
dgCommon =
new DataGridCommon
(
new DataGridCommon.SetDataGridSourceHandler(BindDataGrid),
new DataGridCommon.SetDataGridSortHandler(SortDataGrid),
new DataGridCommon.SetDataGridItemCreateHandler(SetItemCreated)
);
}
处理DataGrid的方法
}
using System.Data;
using System.Collections;
using System.Text.RegularExpressions;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// Web表现层类的基类。
/// </summary>
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
public class BasePage : System.Web.UI.Page
{
/// <summary>
/// 处理DataGrid的对象。
/// </summary>
/// <author>天志</author>
/// <log date="2006-05-24">创建</log>
protected DataGridCommon dgCommon = null;
public BasePage() : base()
{
// 注册处理DataGrid对象的方法
dgCommon =
new DataGridCommon
(
new DataGridCommon.SetDataGridSourceHandler(BindDataGrid),
new DataGridCommon.SetDataGridSortHandler(SortDataGrid),
new DataGridCommon.SetDataGridItemCreateHandler(SetItemCreated)
);
}
处理DataGrid的方法
}
页面中的调用:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// 订票列表页面。
/// </summary>
/// <author>天志</author>
/// <log date="2006-05-25">创建</log>
public class Ticket_List : BasePage
{
protected DataGrid dgdList;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.dgCommon.BindDataGrid(dgdList);
}
}
/// <summary>
/// 取得DataGrid的数据源。
/// </summary>
/// <returns></returns>
public override DataView GetData()
{
// 取得数据源数据
return null;
}
}
Web 窗体设计器生成的代码
// 翻页
private void dgdList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dgCommon.PageChange(dgdList, e);
}
// 排序
private void dgdList_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
this.dgCommon.SortDataGrid(dgdList, e);
}
// 设置排序图标
private void dgdList_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
this.dgCommon.SetItemCreated(dgdList, e);
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
/// 订票列表页面。
/// </summary>
/// <author>天志</author>
/// <log date="2006-05-25">创建</log>
public class Ticket_List : BasePage
{
protected DataGrid dgdList;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
this.dgCommon.BindDataGrid(dgdList);
}
}
/// <summary>
/// 取得DataGrid的数据源。
/// </summary>
/// <returns></returns>
public override DataView GetData()
{
// 取得数据源数据
return null;
}
}
Web 窗体设计器生成的代码
// 翻页
private void dgdList_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
this.dgCommon.PageChange(dgdList, e);
}
// 排序
private void dgdList_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
{
this.dgCommon.SortDataGrid(dgdList, e);
}
// 设置排序图标
private void dgdList_ItemCreated(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
this.dgCommon.SetItemCreated(dgdList, e);
}
}