|
代码如下:
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")
{
OnItemAdded();
_itemCount++;
}
else if(eventArgument == "RemoveFromCart")
{
OnItemRemoved();
_itemCount = 0;
}
}
protected override void Render(HtmlTextWriter output)
{
//如果控件处在详细说明模式,呈现一个按钮把它添加到购物车中
if (_location == LoaderType.DetailLoader)
{
output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(this,"AddToCart"));
output.AddAttribute(HtmlTextWriterAttribute.Style,"Display:inline;BORDER-RIGHT:gray thin outset;BORDER-TOP:gray thin outset;BORDER-LEFT:gray thin outset;CURSOR:hand;BORDER-BOTTOM:gray thin outset;FONT-FAMILY:'ComicSans MS';BACKGROUND-COLOR:lightgrey");
output.RenderBeginTag(HtmlTextWriterTag.Div);
output.Write("Add to cart");
output.RenderEndTag();
}
else//现在在购物车详细说明页面,因此显示两个按钮和一个文本框
{
output.AddAttribute(HtmlTextWriterAttribute.Width,"5");
output.AddAttribute(HtmlTextWriterAttribute.Name,this.UniqueID);
output.AddAttribute(HtmlTextWriterAttribute.Value,_itemCount.ToString());
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
//呈现删除按钮
output.AddAttribute(HtmlTextWriterAttribute.Style,"Display:inline;BORDER-RIGHT:gray thin outset;BORDER-TOP:gray thin outset;BORDER-LEFT:gray thin outset;CURSOR:hand;BORDER-BOTTOM:gray thin outset;FONT-FAMILY:'ComicSans MS';BACKGROUND-COLOR:lightgrey");
output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(this,"RemoveFromCart"));
output.RenderBeginTag(HtmlTextWriterTag.Div);
output.Write("Remove");
output.RenderEndTag();
//呈现更新按钮
output.AddAttribute(HtmlTextWriterAttribute.Style,"Display:inline;BORDER-RIGHT:gray thin outset;BORDER-TOP:gray thin outset;BORDER-LEFT:gray thin outset;CURSOR:hand;BORDER-BOTTOM:gray thin outset;FONT-FAMILY:'ComicSans MS';BACKGROUND-COLOR:lightgrey");
output.AddAttribute(HtmlTextWriterAttribute.Onclick,Page.GetPostBackEventReference(this,""));
output.RenderBeginTag(HtmlTextWriterTag.Div);
output.Write("Update");
output.RenderEndTag();
}
}
protected override object SaveViewState()
{
return (object)_itemCount;
}
protected override void LoadViewState(object state)
{
_itemCount = (int)state;
}
}
}
当点击Add to Cart,Remove,Update会引发
LoadPostData(string postDataKey,NameValueCollection postData)方法
会进行判断原始数量是否用户刚输入数量是否相同.如果不同的话返回真.执行下列方法
public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == "AddToCart")
{
OnItemAdded();
_itemCount++;
}
else if(eventArgument == "RemoveFromCart")
{
OnItemRemoved();
_itemCount = 0;
}
}
但此方法
public void RaisePostDataChangedEvent()
{
OnItemUpdated();
}
当单击Update首先执行LoadPostData方法.会判断文本框内原始值是否等于用户输入的值.如果等于的话返回假.不会执行RaisePostBackEvent方法.而是执行
public void RaisePostDataChangedEvent()
{
OnItemUpdated();
}
这个方法.不知小弟理解的正确不正确.只是心里有点疑惑.或着哪位耐心看完这段程序后.给我讲一个完整的流程.THANK YOU |
|