扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
// OnBubbleEvent方法定义 protected virtual bool OnBubbleEvent(object source,EventArgs args){ return false;} // RaiseBubbleEvent方法定义 protected void RaiseBubbleEvent(object source,EventArgs args){ Control currentTarget = _parent; while(currentTarget != null) { if(currentTarget.OnBubbleEvent(source,args) { return; } currentTarget = currentTarget.Parent; } } |
protected override bool OnBubbleEvent(object sender,EventArgs e){ bool handled = false; if(e is CommandEventArgs) { CommandEventArgs ce = (CommandEventArgs)e; if(ce.CommandName == "ButtonClick") { OnButtonClick(EventArgs.Empty); handled =true; } } return handled; } |
protected override bool OnBubbleEvent(object sender,EventArgs e){ if(e is CommandEventArgs) { CommandEventArgs ce = (CommandEventArgs)e; RaiseBubbleEvent(this,ce); return true; } return false; } |
using System; using System.Web.UI; using System.Web.UI.WebControls; using System.ComponentModel; using System.ComponentModel.Design; namespace WebControlLibrary{ public class CompositeEvent : CompositeControl { //声明变量 private Button _button; private TextBox _textBox; private static readonly object EventSubmitKey = new object(); //定义属性ButtonText,用于指定按钮上的文字 [ Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置显示显示在按钮上的文字") ] public string ButtonText { ...... } //定义属性Text,表示文本框的输入 [ Bindable(true), Category("Appearance"), DefaultValue(""), Description("获取或设置文本框输入文本") ] public string Text { ...... } // 实现事件属性结构 public event EventHandler Submit { add { Events.AddHandler(EventSubmitKey, value); } remove { Events.RemoveHandler(EventSubmitKey, value); } } // 实现OnSubmit protected virtual void OnSubmit(EventArgs e) { EventHandler SubmitHandler = (EventHandler)Events[EventSubmitKey]; if (SubmitHandler != null) { SubmitHandler(this, e); } } // 删除_button_Click // 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法 protected override void RecreateChildControls() { ...... } //重写CreateChildControls方法,将子控件添加到复合控件中 protected override void CreateChildControls() { Controls.Clear(); _button = new Button(); _textBox = new TextBox(); _button.ID = "btn"; _button.CommandName = "Submit"; this.Controls.Add(_button); this.Controls.Add(_textBox); } // 重写OnBubbleEvent方法,执行事件冒泡 protected override bool OnBubbleEvent(object source, EventArgs e) { bool handled = false; if (e is CommandEventArgs) { CommandEventArgs ce = (CommandEventArgs)e; if (ce.CommandName == "Submit") { OnSubmit(EventArgs.Empty); handled = true; } } return handled; } //重写Render方法,呈现控件中其他的HTML代码 protected override void Render(HtmlTextWriter output) { ...... } } } |
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者