Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1150829
  • 博文数量: 141
  • 博客积分: 3161
  • 博客等级: 中校
  • 技术积分: 3011
  • 用 户 组: 普通用户
  • 注册时间: 2011-09-27 14:53
文章存档

2012年(28)

2011年(113)

分类: 系统运维

2011-09-29 12:17:01

                             CompositeControl  后篇 --事件冒泡

 

    我们上一篇已经开发一个登录的组合控件,但是我们把控件的事件的这个问题没有解决,现在我们就来看看这个问题。

 

    因为我们的组合控件Login是由一些已经存在的服务器控件的组合的,这样就省却了我们很多的麻烦,上篇中我们已经把子控件的一些属性,如Text等,显示呈现为了组合控件的属性。我们将之称为“属性的冒泡”(我自己这样认为的)。

 

    我们现在来想下:其实我们点击“提交”按钮,我们就想把控件中信息发送给服务器,然后验证。即,我们点击的是“提交”按钮,但是触发的确实Login控件的验证事件。也就是说,我们应该把子控件-button的事件,上升冒泡成为Login的事件。

 

    现在我们就正式讲讲怎么来事件冒泡。

 

    对于事件的一些步骤,大家应该还记得:    

      

    1。定义一个数据类,用来当发生事件时,我们把一些信息携带。在本控件中,当我们点击“提交”按钮时,我们就把用户名和密码作为数据送到服务器。所以我们的数据类包含两个信息:如下:

 

  1. public class ValidateEventArgs : EventArgs
  2. {
  3.     private string userName;
  4.     private string userPassward;

  5.     public string UserName
  6.     {
  7.         get
  8.         {
  9.             return userName;
  10.         }
  11.     }

  12.     public string UserPassward
  13.     {
  14.         get
  15.         {
  16.             return userPassward;
  17.         }
  18.     }

  19.     public ValidateEventArgs(string username, string userpassward)
  20.     {
  21.         this.userName = username;
  22.         this.userPassward = userpassward;
  23.     }
  24. }

 

    相信大家对上面的代码不陌生!!!

 

    2.我们来定义一个委托:

 

  1. public delegate void ValidateEventHandler(object sender,ValidateEventArgs args);

 

    3.定义事件。

  1. private static readonly object ValidateEventKey = new object();
  2.     public event ValidateEventHandler ValidateUserInfoChanged
  3.     {
  4.         add
  5.         {
  6.             Events.AddHandler(ValidateEventKey, value);
  7.         }
  8.         remove
  9.         {
  10.             Events.RemoveHandler(ValidateEventKey, value);
  11.         }
  12.     }

  13.     protected virtual void OnValidateChanged(ValidateEventArgs args)
  14.     {
  15.         ValidateEventHandler handler = Events[ValidateEventKey] as ValidateEventHandler;
  16.         if (handler != null)
  17.             handler(this, args);
  18.     }

 

    4.好了;准备都做好了,下面我们只要把这个事件冒泡成为Login的控件的就行了。其实很简单的。只要重写一个方法就行了。如下:

   

  1. protected override bool OnBubbleEvent(object source, EventArgs args)
  2.     {
  3.         bool handled = false;

  4.         CommandEventArgs ce = args as CommandEventArgs;
  5.         if (ce != null && ce.CommandName == "Validate")
  6.         {
  7.             handled = true;
  8.             string userName = this.txtUserName.Text;
  9.             string userPassward = this.txtUserPassward.Text;

  10.             //把信息输入

  11.             ValidateEventArgs ve = new ValidateEventArgs(userName, userPassward);
  12.             OnValidateChanged(ve);
  13.         }
  14.         return handled;
  15.     }

    好了,完了,就这样了。

    完整代码附上:

 

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.ComponentModel;

  8. namespace LoginControl
  9. {
  10.     public class Login : CompositeControl
  11.     {
  12.         #region 要组合的控件
  13.         Label lbUserName;
  14.         Label lbUserPassward;
  15.         TextBox txtUserName;
  16.         TextBox txtUserPassward;
  17.         Button submitButton;
  18.         #endregion


  19.         #region 重写方法CreateChildControls

  20.         protected override void CreateChildControls()
  21.         {
  22.             Controls.Clear();

  23.             //初始化控件lbUserName

  24.             lbUserName = new Label();
  25.             lbUserName.Text = "用户名:";
  26.             lbUserName.ID = "lbUserName";
  27.             //把控件添加到我们的组合控件中

  28.             Controls.Add(lbUserName);

  29.             //初始化控件lbUserPassward

  30.             lbUserPassward = new Label();
  31.             lbUserPassward.Text = "密 码:";
  32.             lbUserPassward.ID = "lbUserPassward";
  33.             Controls.Add(lbUserPassward);


  34.             txtUserName = new TextBox();
  35.             txtUserName.ID = "txtUserName";
  36.             txtUserName.Width = Unit.Percentage(60);
  37.             Controls.Add(txtUserName);

  38.             txtUserPassward = new TextBox();
  39.             txtUserPassward.ID = "txtUserPassward";
  40.             txtUserPassward.Width = Unit.Percentage(60);
  41.             Controls.Add(txtUserPassward);

  42.             submitButton = new Button();
  43.             submitButton.Text = "提交";
  44.             submitButton.CommandName = "Validate";
  45.             Controls.Add(submitButton);

  46.             ChildControlsCreated = true;
  47.         }
  48.         #endregion
  49.         #region 将组合的子控件的属性 呈现为 组合控件的属性

  50.         public string UserNameLabelText
  51.         {
  52.             get
  53.             {
  54.                 EnsureChildControls();
  55.                 return lbUserName.Text;
  56.             }
  57.             set
  58.             {
  59.                 EnsureChildControls();
  60.                 lbUserName.Text = value;
  61.             }
  62.         }

  63.         public string UserPasswardLabelText
  64.         {
  65.             get
  66.             {
  67.                 EnsureChildControls();
  68.                 return lbUserPassward.Text;
  69.             }
  70.             set
  71.             {
  72.                 EnsureChildControls();
  73.                 lbUserPassward.Text = value;
  74.             }
  75.         }

  76.         public string SubmitButtonText
  77.         {
  78.             get
  79.             {
  80.                 EnsureChildControls();
  81.                 return submitButton.Text;
  82.             }
  83.             set
  84.             {
  85.                 EnsureChildControls();
  86.                 submitButton.Text = value;
  87.             }
  88.         }


  89.         #endregion

  90.         #region 组合控件呈现的样式
  91.         protected override HtmlTextWriterTag TagKey
  92.         {
  93.             get
  94.             {
  95.                 return HtmlTextWriterTag.Table;
  96.             }
  97.         }

  98.         protected override void RenderContents(HtmlTextWriter writer)
  99.         {

  100.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);

  101.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
  102.             lbUserName.RenderControl(writer);
  103.             writer.RenderEndTag();//td的结束


  104.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
  105.             txtUserName.RenderControl(writer);
  106.             writer.RenderEndTag();

  107.             writer.RenderEndTag();//tr的结束


  108.             //***********************************************


  109.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);

  110.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
  111.             lbUserPassward.RenderControl(writer);
  112.             writer.RenderEndTag();//td的结束


  113.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
  114.             txtUserPassward.RenderControl(writer);
  115.             writer.RenderEndTag();

  116.             writer.RenderEndTag();//tr的结束


  117.             //***********************************************


  118.             writer.RenderBeginTag(HtmlTextWriterTag.Tr);

  119.             writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
  120.             writer.AddAttribute(HtmlTextWriterAttribute.Align, "center");
  121.             writer.RenderBeginTag(HtmlTextWriterTag.Td);
  122.             submitButton.RenderControl(writer);
  123.             writer.RenderEndTag();

  124.             writer.RenderEndTag();//tr的结束





  125.         }
  126.         #endregion

  127.         #region 事件
  128.         private static readonly object ValidateEventKey = new object();
  129.         public event ValidateEventHandler ValidateUserInfoChanged
  130.         {
  131.             add
  132.             {
  133.                 Events.AddHandler(ValidateEventKey, value);
  134.             }
  135.             remove
  136.             {
  137.                 Events.RemoveHandler(ValidateEventKey, value);
  138.             }
  139.         }

  140.         protected virtual void OnValidateChanged(ValidateEventArgs args)
  141.         {
  142.             ValidateEventHandler handler = Events[ValidateEventKey] as ValidateEventHandler;
  143.             if (handler != null)
  144.                 handler(this, args);
  145.         }

  146.         #endregion

  147.         #region 冒泡

  148.         protected override bool OnBubbleEvent(object source, EventArgs args)
  149.         {
  150.             bool handled = false;

  151.             CommandEventArgs ce = args as CommandEventArgs;
  152.             if (ce != null && ce.CommandName == "Validate")
  153.             {
  154.                 handled = true;
  155.                 string userName = this.txtUserName.Text;
  156.                 string userPassward = this.txtUserPassward.Text;

  157.                 //把信息输入

  158.                 ValidateEventArgs ve = new ValidateEventArgs(userName, userPassward);
  159.                 OnValidateChanged(ve);
  160.             }
  161.             return handled;
  162.         }
  163.         #endregion



  164.     }
  165. }
阅读(694) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~