asp.net中的CustomControl(Dropdownlist)

时间:2010-03-04 12:14:44

标签: asp.net

如何使用自定义控件(下拉列表)中的代理编写事件

1 个答案:

答案 0 :(得分:0)

以下是自定义控件中使用事件的示例:

  using System;
  using System.Web.UI;

  namespace CustomControls 
  {  
     public class MyButton: Control, IPostBackEventHandler 
     {     
        // Defines the Click event.
        public event EventHandler Click;

        // OnClick dispatches the event to delegates that
        // are registered with the Click event.
        // Controls that derive from MyButton can handle the
        // Click event by overriding OnClick
        // instead of attaching a delegate. The event data
        // is passed as an argument to this method.
        protected virtual void OnClick(EventArgs e) 
        {     
           if (Click != null) 
           {
              Click(this, e);
           }  
        }

        // Method of IPostBackEventHandler that raises change events.
        public void RaisePostBackEvent(string eventArgument)
        {     
           OnClick(EventArgs.Empty);
        }

        protected override void Render(HtmlTextWriter output) 
        {     
           output.Write("<INPUT TYPE = submit name = " + this.UniqueID + 
              " Value = 'Click Me' />"); 
        }
     }    
  }