关于控件事件流程
代码如下:
using System;
using System.Collections.Specialized;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace 购物车
{
/// <summary>
/// WebCustomControl1 的摘要说明。
/// </summary>
public enum LoaderType{DetailLoader,CartLoader};
public delegate void CartLoaderEventHandler(object sender,CartLoaderEventArgs e);
public class CartLoaderEventArgs:EventArgs
{
private int _itemCount;
private string _productNumber;
public CartLoaderEventArgs(){}
public CartLoaderEventArgs(string ProductNumber)
{
_itemCount = 0;
_productNumber = ProductNumber;
}
public CartLoaderEventArgs(string ProductNumber,int NumberOfItems)
{
_itemCount = NumberOfItems;
_productNumber = ProductNumber;
}
public string ProductNumber
{
get{return _productNumber;}
set{_productNumber = value;}
}
public int NumberOfItems
{
get{return _itemCount;}
set{_itemCount = value;}
}
}
public class CartLoader : System.Web.UI.WebControls.WebControl,IPostBackDataHandler,IPostBackEventHandler
{
public event CartLoaderEventHandler ItemAdded;
public event CartLoaderEventHandler ItemUpdated;
public event CartLoaderEventHandler ItemRemoved;
private LoaderType _location = LoaderType.DetailLoader;
private string _productID;
private int _itemCount;
public LoaderType Location
{
get{return _location;}
set{_location = value;}
}
public string ProductID
{
get{return _productID;}
set{_productID = value;}
}
public int ItemCount
{
get{return _itemCount;}
set{_itemCount = value;}
}
protected void OnItemAdded()
{
if(ItemAdded != null)
ItemAdded(this,new CartLoaderEventArgs(_productID,_itemCount));
}
protected void OnItemUpdated()
{
if (ItemUpdated != null)
ItemUpdated(this,new CartLoaderEventArgs(_productID,_itemCount));
}
protected void OnItemRemoved()
{
if (ItemRemoved != null)
ItemRemoved(this,new CartLoaderEventArgs(_productID));
}
//加载文本框数据时引发
public bool LoadPostData(string postDataKey,NameValueCollection postData)
{
int count = Int32.Parse(postData[postDataKey]);
if (_itemCount != count)
{
_itemCount = count;
return true;
}
else
{
return false;
}
}
//如果控件启动回送.也就是有一个按钮被单击.调用此方法
public void RaisePostDataChangedEvent()
{
OnItemUpdated();
}
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == "AddToCart ")