sharepoint2007重写SaveButton的方法已经有了,2010只要改几个地方就可以了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.SharePoint;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.Utilities;
using System.Globalization;
using System.Collections;
using System.Web;

namespace ***.***.***.WebControls
{
    public class BeforeSaveItemArgs : EventArgs
    {
        public bool CanRedirect { get; set; }
        public SPListItem ListItem { get; set; }
        public bool Cancel { get; set; }
    }

    public class AfterSaveItemArgs : EventArgs
    {
        public SPListItem ListItem { get; set; }
        public bool Success { get; set; }
    }

    public delegate void BeforeSaveItemHandler(object sender, BeforeSaveItemArgs e);
    public delegate void AfterSaveItemHandler(object sender, AfterSaveItemArgs e);

    public class UpdateButton : Microsoft.SharePoint.WebControls.SaveButton//FormComponent 
    {
        private SPContentType m_ct;
        private SPContentType m_oldCt;
        private bool _canRedirect = true;
        private static readonly object EventBeforeSaveItem = new object();
        private static readonly object EventAfterSaveItem = new object();

        public event BeforeSaveItemHandler OnBeforeSaveItem
        {
            add
            {
                base.Events.AddHandler(EventBeforeSaveItem, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventBeforeSaveItem, value);
            }
        }

        public event AfterSaveItemHandler OnAfterSaveItem
        {
            add
            {
                base.Events.AddHandler(EventAfterSaveItem, value);
            }
            remove
            {
                base.Events.RemoveHandler(EventAfterSaveItem, value);
            }
        }

        protected virtual void DoBeforeSaveItem(BeforeSaveItemArgs e)
        {
            BeforeSaveItemHandler handler = (BeforeSaveItemHandler)base.Events[EventBeforeSaveItem];
            if (handler != null)
            {
                handler(this, e);

                _canRedirect = e.CanRedirect;
            }
        }

        protected virtual void DoAfterSaveItem(AfterSaveItemArgs e)
        {
            AfterSaveItemHandler handler = (AfterSaveItemHandler)base.Events[EventAfterSaveItem];
            if (handler != null)
            {
                handler(this, e);
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
        }


        protected override bool SaveItem()
        {

            SPListItem item = base.ItemContext.ListItem;

            BeforeSaveItemArgs beforeSaveArgs =
                new BeforeSaveItemArgs() { ListItem = item, Cancel = false, CanRedirect = false };

            DoBeforeSaveItem(beforeSaveArgs);

            if (!beforeSaveArgs.Cancel)
            {
                bool result = base.SaveItem();

                AfterSaveItemArgs afterSaveArgs = new AfterSaveItemArgs()
                {
                    ListItem = item,
                    Success = result
                };
                DoAfterSaveItem(afterSaveArgs);
                return result;
            }
            else
                return false;
        }


        public bool CanRedirect
        {
            get { return _canRedirect; }
            set { _canRedirect = value; }
        }

        public bool Update()
        {

            bool flag = false;

            this.Page.Validate();
            if (this.Page.IsValid)
            {
                try
                {
                    flag = SaveItem();

                }
                catch (Exception ex)
                {
                    throw new SPException(ex.Message, ex);
                }

                string redirectUrl = base.RedirectUrl;

                this.Context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");

                this.Context.Response.Flush();
                this.Context.Response.End();
            }
            return flag;
        }

        #region internal method
        private static IFormatProvider s_ifp;

        internal static IFormatProvider FormatProvider
        {
            get
            {
                if (s_ifp == null)
                {
                    StringWriter writer = new StringWriter(
                        System.Globalization.CultureInfo.InvariantCulture);
                    s_ifp = writer.FormatProvider;
                }
                return s_ifp;
            }
        }

  
        internal SPContentType GetContentTypeThroughQueryString()
        {
            string str = this.Page.Request.QueryString["ContentTypeId"];
            if (str != null)
            {
                SPContentType type2;
                try
                {
                    if (str.StartsWith("0x"))
                    {
                        SPContentTypeId contentTypeId = new SPContentTypeId(str);
                        SPContentType type = this.List.ContentTypes[contentTypeId];
                        if (type == null)
                        {
                            return this.List.ContentTypes[this.List.ContentTypes.BestMatch(contentTypeId)];
                        }
                        return type;
                    }
                    int num = int.Parse(str, UpdateButton.FormatProvider);
                    type2 = this.List.ContentTypes[num];
                }
                catch (ArgumentException)
                {
                    return null;
                }
                catch (SPException)
                {
                    return null;
                }
                return type2;
            }
            return null;
        }

        internal SPContentType ContentType
        {
            get
            {
                if (this.m_ct == null)
                {
                    if (this.Item is SPListItem)
                    {
                        if (this.ItemContext.FormContext.FormMode != SPControlMode.Display)
                        {
                            this.m_ct = this.GetContentTypeThroughQueryString();
                            if (this.m_ct == null)
                            {
                                if (this.ItemContext.FormContext.FormMode != SPControlMode.Edit)
                                {
                                    foreach (SPContentType type in this.List.ContentTypes)
                                    {
                                        if (!(type.Id.Parent == SPBuiltInContentTypeId.Folder))
                                        {
                                            this.m_ct = type;
                                            break;
                                        }
                                    }
                                }
                                else
                                    this.m_ct = this.OldContentType;
                            }
                        }
                        else
                        {
                            this.m_ct = this.OldContentType;
                        }
                    }

                    ///00D2////
                    if ((this.m_ct == null) && (this.List.ContentTypes.Count > 0))
                    {
                        this.m_ct = this.List.ContentTypes[0];
                    }
                }
                return this.m_ct;
            }
        }

        internal bool IsCurrentItemVersion
        {
            get
            {
                if (this.ItemVersion != null)
                    return (this.ItemVersion.VersionId == 0) ||
                        (((int)this.ListItem["_UIVersion"]) == this.ItemVersion.VersionId);
                else
                    return (int)(this.ListItem["_UIVersion"]) == 0;
            }
        }

        internal SPListItemVersion ItemVersion
        {
            get { return this.ItemContext.ListItemVersion; }
        }


        private SPContentType OldContentType
        {
            get
            {
                if (this.m_oldCt == null)
                {
                    if ((this.ItemContext.FormContext.FormMode != SPControlMode.Edit)
                        && (this.ItemContext.FormContext.FormMode != SPControlMode.Display))
                    {
                        return null;
                    }
                    if (this.Item is SPListItem)
                    {
                        object obj2 = null;
                        try
                        {
                            obj2 = this.ListItem["ContentTypeId"];

                            //if (!this.IsCurrentItemVersion)
                            //{
                            //    obj2 = this.ItemContext.ListItemVersion["ContentTypeId"];
                            //}
                            //else
                            //{
                            //    obj2 = this.ListItem["ContentTypeId"];
                            //}
                        }
                        catch (ArgumentException)
                        {
                        }
                        if (obj2 != null)
                        {
                            SPContentTypeId id = (SPContentTypeId)obj2;
                            this.m_oldCt = this.List.ContentTypes[id];
                        }
                    }
                }
                else
                {
                    return this.m_oldCt;
                }
                return this.m_oldCt;
            }
        }
        #endregion
    }
    public class SaveButton : UpdateButton
    {
        protected override bool OnBubbleEvent(object source, EventArgs e)
        {
            bool flag = false;
            if (e is CommandEventArgs)
            {
                CommandEventArgs args = e as CommandEventArgs;
                if (args.CommandName == "SaveItem")
                {
                    flag = this.Update();
                    return flag;
                }
            }
            return flag;
        }
    }
}

但保存前对一些特殊字段校验怎么办呢?因为这个savebutton只能回到新建的页面,那么填写的都白填写了,本来想在代码里面控制一下的(就是那个写Cancel的原因)但发现没什么用。

最后用什么搞定了呢?用了一个CustomValidator就可以了。

<asp:CustomValidator ID="CustomValidator1" runat="server"
        ControlToValidate="" EnableClientScript="False"
        ErrorMessage="输入有错误"
        onservervalidate="CustomValidator1_ServerValidate"></asp:CustomValidator>


  protected void CustomValidator1_ServerValidate(object source, ServerValidateEventArgs args)
        {
          if (Convert.ToDateTime(FormField15.Value)>System.DateTime.Now)
            { 

          args.IsValid = false;
            }
        }

这样这个savebutton在保存前,保存后就都好用了。,先这么写,后面写详细点。