ASP.NET自定义控件事件响应

1.重载protected override bool OnBubbleEvent(object source, EventArgs args)
OnBubbleEvent是控件内包含的控件向外层控件作事件冒泡
-------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

    public class uc_DanXuan : CompositeControl
    {
        private Button btnSave = null;                          
        public EventHandler SaveAnswer;      //定义这个是为了在控件外进行事件响应处理
     
        protected override void CreateChildControls()
        {
            Controls.Clear();
            btnSave = new Button();
            btnSave.Text = "Click Me";
            btnSave.CommandName = "事件的名字随便取";   //这是事件发生时,事件的名字,区分事件用
            btnSave.CommandArgument = "事件的参数,看需要"; //这是事件发生后,用来传参数的,可以没有
            Controls.Add(btnSave);
        }

        protected override bool OnBubbleEvent(object source, EventArgs args)
        {
            if (args is CommandEventArgs)
            {
                CommandEventArgs cmdArgs = args as CommandEventArgs;
                                                         //可以在这里加些代码,在控件内先处理一下
                if(SaveAnswer != null)                   //如果要在外面处理,就到事件处理程序中去
                    SaveAnswer(source, args);           
                return true;
            }
            else
            {
                return base.OnBubbleEvent(source, args);
            }

        }
    }
--------------------------------------------------------------------------------------------------------------------------------------

测试它:
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        uc_DanXuan ucdx = new uc_DanXuan();
        ucdx.SaveAnswer += new EventHandler(SaveAnswer);             //要在外面处理事件
        form1.Controls.Add(ucdx);

    }
    public void SaveAnswer(object sender, EventArgs args)               //这里具体处理
    {

    }
}
---------------------------------------------------------------------------------------------------------------------------------------------

2.直接响应内部控件的事件
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

public class uc_DanXuan : CompositeControl
{
   private Button btnSave;
   public event EventHandler SaveAnswer;               //留给控件外部处理这个事件

   protected virtual void OnSave(Object source, EventArgs e)   //控件内部在这里处理这个事件
   {
       if (SaveAnswer != null)
       {
           SaveAnswer(this, e);                       //内部处理完了,再给外部处理
       }
   }
   
    protected override void CreateChildControls()
    {
        Controls.Clear();
        btnSave = new Button();
        btnSave.ID = "btn";
        btnSave.Text = "Save";
        btnSave.Click += new EventHandler(OnSave);    //响应按钮的click
        this.Controls.Add(btnSave);
    }

    protected override void Render(HtmlTextWriter writer)
    {
        btnSave.RenderControl(writer);    
    }
}
测试代码同上

----------------------------------------------------------------------------------------------------------------------------------------
3.利用客户端javascript来回发?
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

public class uc_DanXuan : CompositeControl, IPostBackEventHandler
{
    private Button btnSave;
    public event EventHandler SaveAnswer;               //留给控件外部处理这个事件

    protected virtual void OnSave(EventArgs e)   //控件内部在这里处理这个事件
    {
        if (SaveAnswer != null)
        {
            SaveAnswer(this, e);                      //内部处理完了,再给外部处理
        }
    }

    protected override void CreateChildControls()
    {
        btnSave = new Button();
        btnSave.ID = "btn";
        btnSave.Text = "Save";

        //实用客户端javascript来完成事件回发
        btnSave.OnClientClick = Page.ClientScript.GetPostBackEventReference(this, "save");

    }
    public override void RenderControl(HtmlTextWriter writer)
    {
        btnSave.RenderControl(writer);
        base.RenderControl(writer);
    }

    void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
    {
        if (eventArgument == "save")
            OnSave(new EventArgs());
    }  
}
测试代码同上

4.让TextBox响应onKeyDown
----------------------------------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;

public class uc_DanXuan : CompositeControl, IPostBackEventHandler
{
    private TextBox tbx;

    public event EventHandler OnKeyDown;
  
    protected override void CreateChildControls()
    {
        Controls.Clear();
        tbx = new TextBox();
        tbx.ID = "tbx";
        tbx.Attributes.Add("onkeydown", Page.ClientScript.GetPostBackEventReference(this, "onKeyDown"));
        Controls.Add(tbx);
    }

    public void RaisePostBackEvent(string eventArgument)
    {
        switch (eventArgument)
        {
            case "onKeyDown":
                if (OnKeyDown != null)
                    OnKeyDown(this, EventArgs.Empty);
                break;
        }
    }
}
测试代码:
protected void Page_Load(object sender, EventArgs e)
    {
        uc_DanXuan ucdx = new uc_DanXuan();
        ucdx.OnKeyDown += new EventHandler(ucdx_OnKeyDown);
        form1.Controls.Add(ucdx);

    }

    void ucdx_OnKeyDown(object sender, EventArgs e)
    {
       
    }

posted on 2009-08-24 20:21  rex.ying  阅读(688)  评论(0编辑  收藏  举报