摘要:控制項學習:氣泡事件(BubbleEvent)
1.實作CompositeControl
public class SearchControlBubbleUp : CompositeControl
{
2.覆寫CreateChildControls 加入子控制項
protected override void CreateChildControls()
{
this.Controls.Clear();
btnSearch = new Button();
btnSearch.ID = "btn";
btnSearch.Text = "搜索";
btnSearch.CommandName = "ButtonSearchClick";
tbSearchText = new TextBox();
tbSearchText.ID = "tb";
this.Controls.Add(btnSearch);
this.Controls.Add(tbSearchText);
}
{
this.Controls.Clear();
btnSearch = new Button();
btnSearch.ID = "btn";
btnSearch.Text = "搜索";
btnSearch.CommandName = "ButtonSearchClick";
tbSearchText = new TextBox();
tbSearchText.ID = "tb";
this.Controls.Add(btnSearch);
this.Controls.Add(tbSearchText);
}
3.實作OnBubbleEvent
實點在第7行if (ce.CommandName == "ButtonSearchClick") 的判斷為true後,回傳handled=true
則onBublleEvent則不在往上傳遞
01 protected override bool OnBubbleEvent(object sender, EventArgs e)
02 {
03 bool handled = false;
04 if (e is CommandEventArgs)
05 {
06 CommandEventArgs ce = (CommandEventArgs)e;
07 if (ce.CommandName == "ButtonSearchClick")
08 {
09 SearchEventArgs args = new SearchEventArgs();
10 args.SearchValue = this.Text;
11 OnButtonSearchClick(args);
12 handled = true;
13 }
14 }
15 this.RaiseBubbleEvent(sender, e);
16 return handled;
17 }
02 {
03 bool handled = false;
04 if (e is CommandEventArgs)
05 {
06 CommandEventArgs ce = (CommandEventArgs)e;
07 if (ce.CommandName == "ButtonSearchClick")
08 {
09 SearchEventArgs args = new SearchEventArgs();
10 args.SearchValue = this.Text;
11 OnButtonSearchClick(args);
12 handled = true;
13 }
14 }
15 this.RaiseBubbleEvent(sender, e);
16 return handled;
17 }
Ps:個人學習片段記錄,程式並不完整