Sharepoint 2010 控件系统 之 扩展 SaveButton
上一篇介绍了Sharepoint控件的基本应用,这一篇介绍一下SaveButton的扩展,在扩展之前讲个案例,通过两个字段验证唯一性,这样在开发时就要用到扩展了,但用EventReceive也可以,哪个更好就自己体会吧。
SaveButton目前不支付保存前事件和保存后事件,但是在开发项目时,保存前总要用到验证,因此为了更好的满足要求,合理规划代码就有了扩展这一篇文章,对SaveButton扩展了两个事件,保存前BeforeSaveItem,保存后AfterSaveItem,这两个事件在实际应用中应用比较多。
首先声明事件和委托,如果对事件和委托不清楚可以Google一下,分页就是最好的事例。
/// <summary> /// 保存前 /// </summary> public class BeforeSaveItemArgs : EventArgs { /// <summary>当前列表的ListItem</summary> public SPListItem ListItem { get; set; } /// <summary>默认True,如果是False,则验证通不过,不能保存</summary> public bool Validate { get; set; } /// <summary>当前列表的 List</summary> public SPList List { get; set; } public Dictionary<string, string> Parameters { get; set; } } /// <summary> /// 保存后 /// </summary> public class AfterSaveItemArgs : EventArgs { /// <summary>当前列表的 List</summary> public SPList List { get; set; } /// <summary>当前列表的ListItem</summary> public SPListItem ListItem { get; set; } /// <summary>判断是否保存成功</summary> public bool Success { get; set; } public Dictionary<string, string> Parameters { get; set; } } /// <summary> /// 声明委托 保存前事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void BeforeSaveItemHandler(object sender, BeforeSaveItemArgs e); /// <summary> /// 声明委托 保存后事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void AfterSaveItemHandler(object sender, AfterSaveItemArgs e);
继承SaveButton ,Override SaveItem ,下面为所有的代码
View Code
public class TSaveButton : SaveButton { private static readonly object EventBeforeSaveItem = new object(); private static readonly object EventAfterSaveItem = new object(); #region Event /// <summary> /// 定义保存前事件 /// </summary> public event BeforeSaveItemHandler BeforeSaveItem { add { base.Events.AddHandler(EventBeforeSaveItem, value); } remove { base.Events.RemoveHandler(EventBeforeSaveItem, value); } } /// <summary> /// 定义保存后事件 /// </summary> public event AfterSaveItemHandler AfterSaveItem { add { base.Events.AddHandler(EventAfterSaveItem, value); } remove { base.Events.RemoveHandler(EventAfterSaveItem, value); } } #endregion protected virtual void SaveIteming(BeforeSaveItemArgs e) { BeforeSaveItemHandler handler = (BeforeSaveItemHandler)base.Events[EventBeforeSaveItem]; if (handler != null) { handler(this, e); } } protected virtual void SaveItemed(AfterSaveItemArgs e) { AfterSaveItemHandler handler = (AfterSaveItemHandler)base.Events[EventAfterSaveItem]; if (handler != null) { handler(this, e); } } /// <summary> /// Override SaveItem /// </summary> /// <returns></returns> protected override bool SaveItem() { SPListItem item = base.ItemContext.ListItem; //获取当前Items BeforeSaveItemArgs beforeSaveArgs = new BeforeSaveItemArgs() { ListItem = item, Validate = true, List = base.List }; //构造默认值 SaveIteming(beforeSaveArgs); if (beforeSaveArgs.Validate) //验证保存是否成功 { bool result = base.SaveItem(); //保存成功返回True AfterSaveItemArgs afterSaveArgs = new AfterSaveItemArgs() { ListItem = item, Success = result,List = base.List}; SaveItemed(afterSaveArgs); return result; } else return false; } }
扩展完成之后,就是如何运用了,在上一篇中已经提到了这两个事件 OnBeforeSaveItem OnAfterSaveItem
<T:TSaveButton runat="server" ID="btnSave" OnInit="OnInitSave" OnBeforeSaveItem="BeforeSaveItem" OnAfterSaveItem="AfterSaveItem" />
现在事件已经注册好了,接下来就是写 BeforeSaveItem的方法
#region OnBeforeSaveItem public void BeforeSaveItem(object sender, EventArgs e) { BeforeSaveItemArgs beforeSaveItemArgs = e as BeforeSaveItemArgs; if (beforeSaveItemArgs != null) { beforeSaveItemArgs.Validate = false; if (ValidateCode(beforeSaveItemArgs.ListItem.ID, beforeSaveItemArgs.ListItem.Title, txtCodeKey.Text, beforeSaveItemArgs.List)) { beforeSaveItemArgs.ListItem["ParentId"] = hfParentId.Value; beforeSaveItemArgs.Validate = true; } else { Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ModalHostScript", "alert('代码已存在!');", true); return; } beforeSaveItemArgs.Validate = false; if (ValidateCodeName(beforeSaveItemArgs.ListItem.ID, txtCodeName.Text, txtCodeKey.Text, beforeSaveItemArgs.List)) { beforeSaveItemArgs.Validate = true; } else { Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "ModalHostScript", "alert('名称已存在!');", true); } } } #endregion
就这样完成了SaveButton的扩展。如果有其它特殊要求就自己扩展吧。
参考文章:http://www.cnblogs.com/myheaven/archive/2011/04/29/2032892.html