submit控件的触发编写

Fish Li那里看来的

重载一个Page里面的Onload的方法 添加一个浏览器文件 执行这个方法哦(Page.browser) 是为了添加每个页面的方法添加到一个Hashtable里面

View Code
/// <summary>
        /// 执行方法 重载Onload
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            MethodsAttribute[] methods = GetMethods(Page.GetType().BaseType);
            if (methods.Length == 0)
            {
                return;
            }
            foreach (var m in methods)
            {
                if (!string.IsNullOrEmpty(Page.Request.Params[m.MethodInfo.Name]))
                {
                    m.MethodInfo.Invoke(Page, null);

                    //if (m.SubmitAttribute.IsDoMethod && Page.Response.IsRequestBeingRedirected == false)
                    //{
                    //    Page.Response.Redirect(Page.Request.RawUrl);
                    //}
                    return;
                }
            }

        }
   private static readonly Hashtable table = Hashtable.Synchronized(new Hashtable());//为了存储方法的集合 同样的页面里面同样的方法只存一次
        #region  static readonly 在运行的时候才知道AB的值(它成员可以读写操作哦)(自己本身不可以赋值哦 只能初始化一次)
        //static readonly int A = B * 10;//B的值不知道多少 int 默认为0
        //static readonly int B = 10;//
        //输出结果A=0;B=10;
        //const int a=b*10;//因为a必须初始化 需要先知道a的值 故先得知b=10,a=10*10;
        //const int b=10;//
        //输出结果A=100;B=10;

        #endregion

Page.browser里面添加这个OverrideSubmitChilck我是命名控件 OverrideSubmitClick类

 <browser refID="Default">
    <controlAdapters markupTextWriterType="System.Web.UI.XhtmlTextWriter" >
      <adapter
        controlType="System.Web.UI.Page"
                     adapterType="OverrideSubmitChilck.OverrideSubmitClick" >
      </adapter>
    </controlAdapters>

  </browser>

添加一个属性和一个存方法的类

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class SubmitAttribute : Attribute
    {
        //是否触发事件
        public bool IsDoMethod { get; set; }
    }
    internal sealed class MethodsAttribute
    {
        /// <summary>
        /// 事件是否触发
        /// </summary>
        public SubmitAttribute SubmitAttribute { get; set; }

        /// <summary>
        /// 一个事件
        /// </summary>
        public MethodInfo MethodInfo { get; set; }
    }


完整

View Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Reflection;
using System.Collections;
namespace OverrideSubmitChilck
{
    [AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
    public class SubmitAttribute : Attribute
    {
        //是否触发事件
        public bool IsDoMethod { get; set; }
    }
    internal sealed class MethodsAttribute
    {
        /// <summary>
        /// 事件是否触发
        /// </summary>
        public SubmitAttribute SubmitAttribute { get; set; }

        /// <summary>
        /// 一个事件
        /// </summary>
        public MethodInfo MethodInfo { get; set; }
    }
    public class OverrideSubmitClick : System.Web.UI.Adapters.PageAdapter
    {
        //Hashtable.Synchronized(new Hashtable())也是一个线程安全的集合
        private static readonly Hashtable table = Hashtable.Synchronized(new Hashtable());//为了存储方法的集合 同样的页面里面同样的方法只存一次
        #region  static readonly 在运行的时候才知道AB的值(它成员可以读写操作哦)(自己本身不可以赋值哦 只能初始化一次)
        //static readonly int A = B * 10;//B的值不知道多少 int 默认为0
        //static readonly int B = 10;//
        //输出结果A=0;B=10;
        //const int a=b*10;//因为a必须初始化 需要先知道a的值 故先得知b=10,a=10*10;
        //const int b=10;//
        //输出结果A=100;B=10;

        #endregion



        /// <summary>
        /// 执行方法 重载Onload
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            MethodsAttribute[] methods = GetMethods(Page.GetType().BaseType);
            if (methods.Length == 0)
            {
                return;
            }
            foreach (var m in methods)
            {
                if (!string.IsNullOrEmpty(Page.Request.Params[m.MethodInfo.Name]))
                {
                    m.MethodInfo.Invoke(Page, null);

                    //if (m.SubmitAttribute.IsDoMethod && Page.Response.IsRequestBeingRedirected == false)
                    //{
                    //    Page.Response.Redirect(Page.Request.RawUrl);
                    //}
                    return;
                }
            }

        }
        /// <summary>
        /// 得到方法集合
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private MethodsAttribute[] GetMethods(Type t)
        {
            MethodsAttribute[] methods = table[t.AssemblyQualifiedName] as MethodsAttribute[];
            if (methods == null)
            {
                methods = (from m in t.GetMethods(BindingFlags.Instance | BindingFlags.Public)
                           let Is = m.GetCustomAttributes(typeof(SubmitAttribute), false) as SubmitAttribute[]
                           where Is.Length > 0
                           select new MethodsAttribute
                           {
                               SubmitAttribute = Is[0],
                               MethodInfo = m
                           }).ToArray();
                table[t.AssemblyQualifiedName] = methods;
            }
            return methods;
        }
       
    }

}

页面引用

View Code
/// <summary>
        /// DoSubmit要控件的name值
        /// </summary>
        [SubmitAttribute(IsDoMethod = true)]
        public void DoSubmit()
        {
            Response.Write("<script type=\"text/javascript\">alert('成功!');</script>");
        }

 

 

posted on 2012-10-23 11:00  R.Ray  阅读(168)  评论(0)    收藏  举报

导航