标签:
0.调用代码
protected override void Page_Load(object sender, EventArgs e)
{
//给基类服务接口复制,可不付
if (IsPostBack)
{
if (Session["ReportTable3.Dev.Session"] != null)
{
var dt = (System.Data.DataSet)Session["ReportTable3.Dev.Session"];
ASPxGridView1.DataSource = dt;
ASPxGridView1.DataBind();
}
}
//1.这里AfterSelect 是个委托对象,继承多播委托,这里是在注册,最终执行委托的时候会把委托的内容逐一执行一遍,+=也可称为为委托注册方法实例
this.MyPageBar1.AfterSelect += MyPageBar1_AfterSelect;
base.Page_Load(sender, e);
}
//2.委托方法或者成为委托内容 注册的方法并不会马上执行,事件需要触发,可以传递sender对象和事件参数 e
private void MyPageBar1_AfterSelect(object sender, UserControls.SelectEventArgs e)
{
ASPxGridView1.DataSource = e.DataSource;
ASPxGridView1.DataBind();
}
1.先上核心代码 .ascx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web;
using DevExpress.XtraPrinting.Native;
using Framework;
namespace HraWeb.UserControls
{
public partial class MyPageBar : System.Web.UI.UserControl
{
public delegate void BegBindDataEventHandler(object sender, SelectEventArgs e);
public event BegBindDataEventHandler AfterSelect;
private Contract.IService.IDaoService _dao;
/// <summary>
/// 通用数据库操作
/// </summary>
public virtual Contract.IService.IDaoService Dao
{
get
{
if (_dao == null)
_dao = (Contract.IService.IDaoService)ctx["DaoService"];
return _dao;
}
}
private Spring.Context.IApplicationContext _ctx;
protected Spring.Context.IApplicationContext ctx
{
get
{
if (_ctx == null)
_ctx = Spring.Context.Support.ContextRegistry.GetContext();
return _ctx;
}
}
private int TotalBlock => (PageCount - 1) / unitQ + 1;
public object DataSource
{
get;
set;
}
private int PageCount => (TotalCount - 1) / PageSize + 1;
public int TotalCount { get; set; }
private int PageIndex { get; set; }
public int PageSize { get; set; }
public ASPxGridView ViewSource { get; set; }
public void GridDataBind()
{
if (Session["SearchInfo"] == null)
{
Session["SearchInfo"] = SearchInfo;
}
LoadBlock(CurrentBlock);
if (Session["SearchInfo"] != null)
{
SearchInfo = (QueryInfo)Session["SearchInfo"];
SearchInfo.PageSize = PageSize;
SearchInfo.StartRecord = (PageIndex - 1) * PageSize;
SearchInfo.TotalCount = 1;
}
if (Session["ViewSource"] == null)
{
Session["ViewSource"] = ViewSource;
}
else
{
ViewSource = (ASPxGridView)Session["ViewSource"];
}
DataTable dt = Dao.ExcuteDataSet(SearchInfo).Tables[0];
3.这里对事件主动触发(其实就是调用委托 执行之前注册的方法 并传参 谁触发的object sender,以及传递事件参数 e)
var onAfterSelect = this.AfterSelect;
onAfterSelect?.Invoke(this, new SelectEventArgs(dt));
}
public QueryInfo SearchInfo { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
CurrentBlock = 1;
if (Session["CurrentBlock"] != null)
{
CurrentBlock = int.Parse(Session["CurrentBlock"].ToString());
}
PageIndex = PageIndex == 0 ? 1 : PageIndex;
PageSize = PageSize == 0 ? 6 : PageSize;
TotalCount = 100;
Controls.Add("1", LinkButton1);
Controls.Add("2", LinkButton2);
Controls.Add("3", LinkButton3);
Controls.Add("4", LinkButton4);
Controls.Add("5", LinkButton5);
Controls.Add("6", LinkButton6);
}
public void Btn_Change(object sender)
{
for (int i = 0; i < Controls.Count; i++)
{
Controls[(i + 1).ToString()].Enabled = true;
}
LinkButton btn = (sender as LinkButton);
if (btn != null)
{
btn.Enabled = false;
PageIndex = int.Parse(btn.Text);
GridDataBind();
}
}
/// <summary>
/// 1
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton1_Click(object sender, EventArgs e)
{
Btn_Change(sender);
}
/// <summary>
/// 2
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton2_Click(object sender, EventArgs e)
{
Btn_Change(sender);
}
/// <summary>
/// 3
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton3_Click(object sender, EventArgs e)
{
Btn_Change(sender);
}
/// <summary>
/// 4
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton4_Click(object sender, EventArgs e)
{
Btn_Change(sender);
}
/// <summary>
/// 5
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton5_Click(object sender, EventArgs e)
{
Btn_Change(sender);
}
/// <summary>
/// 6
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void LinkButton6_Click(object sender, EventArgs e)
{
Btn_Change(sender);
}
public int CurrentBlock { get; set; }
private void LoadBlock(int CurrentBlock)
{
Session["CurrentBlock"] = CurrentBlock;
for (int i = 0; i < Controls.Count; i++)
{
//Controls[(i + 1).ToString()].Enabled = true;
Controls[(i + 1).ToString()].Visible = true;
}
int start = (CurrentBlock - 1) * unitQ + 1;
int end = 0;
for (int i = 0; i < unitQ; i++)
{
end = start + i;
if (end < PageCount)
{
Controls[(i + 1).ToString()].Text = end.ToString();
}
else
{
Controls[(i + 1).ToString()].Visible = false;
}
}
}
protected void ImageButtonPre_Click(object sender, ImageClickEventArgs e)
{
if (CurrentBlock > 1)
{
CurrentBlock--;
}
LoadBlock(CurrentBlock);
}
public int unitQ = 6;
public Dictionary<string, LinkButton> Controls = new Dictionary<string, LinkButton>();
protected void ImageButtonNext_Click(object sender, ImageClickEventArgs e)
{
if (CurrentBlock < TotalBlock)
{
CurrentBlock++;
}
LoadBlock(CurrentBlock);
}
}
//4 事件参数的定义 继承EventArgs
public class SelectEventArgs : EventArgs
{
private DataTable _dataSource = null;
public DataTable DataSource
{
get { return _dataSource; }
}
public SelectEventArgs(DataTable data)
{
_dataSource = data;
}
}
}
2.再上前台代码,没什么东西看看而已
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MyPageBar.ascx.cs" Inherits="HraWeb.UserControls.MyPageBar" %>
<asp:ImageButton ID="ImageButtonPre" runat="server" Height="20px" ImageUrl="~/images/previous.png" Width="20px" OnClick="ImageButtonPre_Click" />
<asp:LinkButton ID="LinkButton1" Text="1" runat="server" OnClick="LinkButton1_Click">1</asp:LinkButton>
<asp:LinkButton ID="LinkButton2" Text="2" runat="server" OnClick="LinkButton2_Click">2</asp:LinkButton>
<asp:LinkButton ID="LinkButton3" Text="3" runat="server" OnClick="LinkButton3_Click">3</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" Text="4" runat="server" OnClick="LinkButton4_Click">4</asp:LinkButton>
<asp:LinkButton ID="LinkButton5" Text="5" runat="server" OnClick="LinkButton5_Click">5</asp:LinkButton>
<asp:LinkButton ID="LinkButton6" Text="6" runat="server" OnClick="LinkButton6_Click">6</asp:LinkButton>
<asp:ImageButton ID="ImageButtonNext" runat="server" ImageUrl="~/images/next.png" Height="20px" Width="20px" OnClick="ImageButtonNext_Click" ViewStateMode="Enabled" />
3.设计器代码(自动生成可以不用管)
//------------------------------------------------------------------------------
// <自动生成>
// 此代码由工具生成。
//
// 对此文件的更改可能导致不正确的行为,如果
// 重新生成代码,则所做更改将丢失。
// </自动生成>
//------------------------------------------------------------------------------
namespace HraWeb.UserControls {
public partial class MyPageBar {
/// <summary>
/// ImageButtonPre 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.ImageButton ImageButtonPre;
/// <summary>
/// LinkButton1 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.LinkButton LinkButton1;
/// <summary>
/// LinkButton2 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.LinkButton LinkButton2;
/// <summary>
/// LinkButton3 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.LinkButton LinkButton3;
/// <summary>
/// LinkButton4 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.LinkButton LinkButton4;
/// <summary>
/// LinkButton5 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.LinkButton LinkButton5;
/// <summary>
/// LinkButton6 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.LinkButton LinkButton6;
/// <summary>
/// ImageButtonNext 控件。
/// </summary>
/// <remarks>
/// 自动生成的字段。
/// 若要进行修改,请将字段声明从设计器文件移到代码隐藏文件。
/// </remarks>
protected global::System.Web.UI.WebControls.ImageButton ImageButtonNext;
}
}
标签:
原文地址:http://www.cnblogs.com/kexb/p/4954979.html