ASP.NET 2.0 服务器控件之复合控件事件

时间:2008-03-27 13:30:12  来源:  作者:
4UN第一天空网络

 [ 4UN第一天空网络
  Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置文本框输入文本") 4UN第一天空网络
  ] 4UN第一天空网络
  public string Text { 4UN第一天空网络
  get { 4UN第一天空网络
  EnsureChildControls(); 4UN第一天空网络
  return _textBox.Text; 4UN第一天空网络
  } 4UN第一天空网络
  set { 4UN第一天空网络
  EnsureChildControls(); 4UN第一天空网络
  _textBox.Text = value; 4UN第一天空网络
  } 4UN第一天空网络
  } 4UN第一天空网络
  // 实现事件属性结构 4UN第一天空网络
  public event EventHandler Submit { 4UN第一天空网络
  add { 4UN第一天空网络
  Events.AddHandler(EventSubmitKey, value); 4UN第一天空网络
  } 4UN第一天空网络
  remove { 4UN第一天空网络
  Events.RemoveHandler(EventSubmitKey, value); 4UN第一天空网络
  } 4UN第一天空网络
  } 4UN第一天空网络
  // 实现OnSubmit 4UN第一天空网络
  protected virtual void OnSubmit(EventArgs e) { 4UN第一天空网络
  EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey]; 4UN第一天空网络
  if (SubmitHandler != null) { 4UN第一天空网络
  SubmitHandler(this, e); 4UN第一天空网络
  } 4UN第一天空网络
  }

  // 实现Submit事件引发的事件处理程序  4UN第一天空网络

private void _button_Click(Object source, EventArgs e) { 4UN第一天空网络
  OnSubmit(EventArgs.Empty); 4UN第一天空网络
  } 4UN第一天空网络
  // 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法 4UN第一天空网络
  protected override void RecreateChildControls() { 4UN第一天空网络
  EnsureChildControls(); 4UN第一天空网络
  } 4UN第一天空网络
  //重写CreateChildControls方法,将子控件添加到复合控件中 4UN第一天空网络
  protected override void CreateChildControls() { 4UN第一天空网络
  Controls.Clear(); 4UN第一天空网络
  _button = new Button(); 4UN第一天空网络
  _textBox = new TextBox(); 4UN第一天空网络
  _button.ID = "btn"; 4UN第一天空网络
  _button.Click += new EventHandler(_button_Click); 4UN第一天空网络
  this.Controls.Add(_button); 4UN第一天空网络
  this.Controls.Add(_textBox); 4UN第一天空网络
  } 4UN第一天空网络
  //重写Render方法,呈现控件中其他的HTML代码 4UN第一天空网络
  protected override void Render(HtmlTextWriter output) { 4UN第一天空网络
  output.AddAttribute(HtmlTextWriterAttribute.Border, "0px"); 4UN第一天空网络
  output.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "5px"); 4UN第一天空网络
  output.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0px"); 4UN第一天空网络
  output.RenderBeginTag(HtmlTextWriterTag.Table); 4UN第一天空网络
  output.RenderBeginTag(HtmlTextWriterTag.Tr); 4UN第一天空网络
  output.RenderBeginTag(HtmlTextWriterTag.Td); 4UN第一天空网络
  _textBox.RenderControl(output); 4UN第一天空网络
  output.RenderEndTag(); 4UN第一天空网络
  output.RenderBeginTag(HtmlTextWriterTag.Td); 4UN第一天空网络
  _button.RenderControl(output); 4UN第一天空网络
  output.RenderEndTag(); 4UN第一天空网络
  output.RenderEndTag(); 4UN第一天空网络
  output.RenderEndTag(); 4UN第一天空网络
  } 4UN第一天空网络
  } 4UN第一天空网络
  }

  如上代码所示,复合控件CompositeEvent中包含两个属性:Text和ButtonText.前者用于获取或者设置文本框中的文本内容,后者用于获取或者设置按钮的显示文本。另外,复合控件类中还实现了一个Submit事件。相关重要逻辑包括:4UN第一天空网络

  第一、在重写CreateChildControls方法中,为子控件Button添加事件处理程序_button_Click.4UN第一天空网络

  第二、和普通的自定义事件一样,为复合控件定义一个顶层事件Submit.这其中包括定义事件属性结构Submit,定义事件处理程序OnSubmit.4UN第一天空网络

  第三、实现_button_Click事件处理程序,调用顶层事件Submit的事件处理程序OnSubmit.4UN第一天空网络

  下面是为测试复合控件CompositeEvent而创建的Default.ASPx文件代码。4UN第一天空网络

   在以上应用中,当用户单击"提交"按钮之后,将引发demo1_Submit处理程序的执行,由此显示文本框输入内容。4UN第一天空网络

文章评论

共有 位天空网友发表了评论 查看完整内容

特别推荐
  • 文字广告
  • 文字广告
  • 文字广告
  • 文字广告
站长黑板报