自定义控件的事件
自定义控件的事件根据需要有两种方式来引发
1
继承IPostBackDataHandler 接口
实现
bool LoadPostData(string postDataKey, NameValueCollection postCollection);
void RaisePostDataChangedEvent();
两个方法,大致都是这样
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string text1 = postCollection [postDataKey] ;
if(text1 != 自定义控件的 text or value or selectID.....)
{
text or value or selectID..... = text1 ;
return true;
}
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
this.OnClick(EventArgs.Empty); //OnSelectedIndexChanged(EventArgs.Empty);.....冒泡出去
}
2
继承IPostBackEventHandler 接口
实现
void RaisePostBackEvent(string eventArgument);
方法
大致实现方法:
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
OnClick(EventArg);..................
}
//给html控件注册脚本.表明回发
我要使用 __doPostBack(eventTarget, eventArgument) 这个方法进行处理.
这个方法进行注册如下:
...Attributes.Add("OnClick", this.Page.GetPostBackClientEvent(this, eventArgument));
如:
button的:
1
继承IPostBackDataHandler 接口
实现
bool LoadPostData(string postDataKey, NameValueCollection postCollection);
void RaisePostDataChangedEvent();
两个方法,大致都是这样
bool IPostBackDataHandler.LoadPostData(string postDataKey, NameValueCollection postCollection)
{
string text1 = postCollection [postDataKey] ;
if(text1 != 自定义控件的 text or value or selectID.....)
{
text or value or selectID..... = text1 ;
return true;
}
return false;
}
void IPostBackDataHandler.RaisePostDataChangedEvent()
{
this.OnClick(EventArgs.Empty); //OnSelectedIndexChanged(EventArgs.Empty);.....冒泡出去
}
2
继承IPostBackEventHandler 接口
实现
void RaisePostBackEvent(string eventArgument);
方法
大致实现方法:
void IPostBackEventHandler.RaisePostBackEvent(string eventArgument)
{
OnClick(EventArg);..................
}
//给html控件注册脚本.表明回发
我要使用 __doPostBack(eventTarget, eventArgument) 这个方法进行处理.
这个方法进行注册如下:
...Attributes.Add("OnClick", this.Page.GetPostBackClientEvent(this, eventArgument));
如:
button的:
protected virtual void RaisePostBackEvent(string eventArgument) { base.ValidateEvent(this.UniqueID, eventArgument); if (this.CausesValidation) { this.Page.Validate(this.ValidationGroup); } this.OnClick(EventArgs.Empty); this.OnCommand(new CommandEventArgs(this.CommandName, this.CommandArgument)); } |
他注册的 事件脚本 在这个方法里面
protected override void AddAttributesToRender(HtmlTextWriter writer)
看代码:
1 private void ProcessPostData(NameValueCollection postData, bool fBeforeLoad)
2 {
3 if (this._changedPostDataConsumers == null)
4 {
5 this._changedPostDataConsumers = new ArrayList();
6 }
7 if (postData != null)
8 {
9 foreach (string str in postData)
10 {
11 if ((str == null) || IsSystemPostField(str))
12 {
13 continue;
14 }
15 Control control = this.FindControl(str);
16 if (control == null)
17 {
18 if (fBeforeLoad)
19 {
20 if (this._leftoverPostData == null)
21 {
22 this._leftoverPostData = new NameValueCollection();
23 }
24 this._leftoverPostData.Add(str, null);
25 }
26 continue;
27 }
28 IPostBackDataHandler postBackDataHandler = control.PostBackDataHandler;
29 if (postBackDataHandler == null)
30 {
31 if (control.PostBackEventHandler != null)
32 {
33 this.RegisterRequiresRaiseEvent(control.PostBackEventHandler);
34 }
35 }
36 else
37 {
38 if ((postBackDataHandler != null) && postBackDataHandler.LoadPostData(str, this._requestValueCollection))
39 {
40 this._changedPostDataConsumers.Add(control);
41 }
42 if (this._controlsRequiringPostBack != null)
43 {
44 this._controlsRequiringPostBack.Remove(str);
45 }
46 }
47 }
48 }
49 ArrayList list = null;
50 if (this._controlsRequiringPostBack != null)
51 {
52 foreach (string str2 in this._controlsRequiringPostBack)
53 {
54 Control control2 = this.FindControl(str2);
55 if (control2 != null)
56 {
57 IPostBackDataHandler handler2 = control2._adapter as IPostBackDataHandler;
58 if (handler2 == null)
59 {
60 handler2 = control2 as IPostBackDataHandler;
61 }
62 if (handler2 == null)
63 {
64 throw new HttpException(SR.GetString("Postback_ctrl_not_found", new object[] { str2 }));
65 }
66 if (handler2.LoadPostData(str2, this._requestValueCollection))
67 {
68 this._changedPostDataConsumers.Add(control2);
69 }
70 continue;
71 }
72 if (fBeforeLoad)
73 {
74 if (list == null)
75 {
76 list = new ArrayList();
77 }
78 list.Add(str2);
79 }
80 }
81 this._controlsRequiringPostBack = list;
82 }
83 }
84
85
2 {
3 if (this._changedPostDataConsumers == null)
4 {
5 this._changedPostDataConsumers = new ArrayList();
6 }
7 if (postData != null)
8 {
9 foreach (string str in postData)
10 {
11 if ((str == null) || IsSystemPostField(str))
12 {
13 continue;
14 }
15 Control control = this.FindControl(str);
16 if (control == null)
17 {
18 if (fBeforeLoad)
19 {
20 if (this._leftoverPostData == null)
21 {
22 this._leftoverPostData = new NameValueCollection();
23 }
24 this._leftoverPostData.Add(str, null);
25 }
26 continue;
27 }
28 IPostBackDataHandler postBackDataHandler = control.PostBackDataHandler;
29 if (postBackDataHandler == null)
30 {
31 if (control.PostBackEventHandler != null)
32 {
33 this.RegisterRequiresRaiseEvent(control.PostBackEventHandler);
34 }
35 }
36 else
37 {
38 if ((postBackDataHandler != null) && postBackDataHandler.LoadPostData(str, this._requestValueCollection))
39 {
40 this._changedPostDataConsumers.Add(control);
41 }
42 if (this._controlsRequiringPostBack != null)
43 {
44 this._controlsRequiringPostBack.Remove(str);
45 }
46 }
47 }
48 }
49 ArrayList list = null;
50 if (this._controlsRequiringPostBack != null)
51 {
52 foreach (string str2 in this._controlsRequiringPostBack)
53 {
54 Control control2 = this.FindControl(str2);
55 if (control2 != null)
56 {
57 IPostBackDataHandler handler2 = control2._adapter as IPostBackDataHandler;
58 if (handler2 == null)
59 {
60 handler2 = control2 as IPostBackDataHandler;
61 }
62 if (handler2 == null)
63 {
64 throw new HttpException(SR.GetString("Postback_ctrl_not_found", new object[] { str2 }));
65 }
66 if (handler2.LoadPostData(str2, this._requestValueCollection))
67 {
68 this._changedPostDataConsumers.Add(control2);
69 }
70 continue;
71 }
72 if (fBeforeLoad)
73 {
74 if (list == null)
75 {
76 list = new ArrayList();
77 }
78 list.Add(str2);
79 }
80 }
81 this._controlsRequiringPostBack = list;
82 }
83 }
84
85
1 internal void RaiseChangedEvents()
2 {
3 if (this._changedPostDataConsumers != null)
4 {
5 for (int i = 0; i < this._changedPostDataConsumers.Count; i++)
6 {
7 Control control = (Control) this._changedPostDataConsumers[i];
8 if (control != null)
9 {
10 IPostBackDataHandler postBackDataHandler = control.PostBackDataHandler;
11 if (((control == null) || control.IsDescendentOf(this)) && ((control != null) && (control.PostBackDataHandler != null)))
12 {
13 postBackDataHandler.RaisePostDataChangedEvent();
14 }
15 }
16 }
17 }
18 }
19
20
21
22
2 {
3 if (this._changedPostDataConsumers != null)
4 {
5 for (int i = 0; i < this._changedPostDataConsumers.Count; i++)
6 {
7 Control control = (Control) this._changedPostDataConsumers[i];
8 if (control != null)
9 {
10 IPostBackDataHandler postBackDataHandler = control.PostBackDataHandler;
11 if (((control == null) || control.IsDescendentOf(this)) && ((control != null) && (control.PostBackDataHandler != null)))
12 {
13 postBackDataHandler.RaisePostDataChangedEvent();
14 }
15 }
16 }
17 }
18 }
19
20
21
22
1 private void RaisePostBackEvent(NameValueCollection postData)
2 {
3 if (this._registeredControlThatRequireRaiseEvent != null)
4 {
5 this.RaisePostBackEvent(this._registeredControlThatRequireRaiseEvent, null);
6 }
7 else
8 {
9 string str = postData["__EVENTTARGET"];
10 bool flag = !string.IsNullOrEmpty(str);
11 if (flag || (this.AutoPostBackControl != null))
12 {
13 Control control = null;
14 if (flag)
15 {
16 control = this.FindControl(str);
17 }
18 if ((control != null) && (control.PostBackEventHandler != null))
19 {
20 string eventArgument = postData["__EVENTARGUMENT"];
21 this.RaisePostBackEvent(control.PostBackEventHandler, eventArgument);
22 }
23 }
24 else
25 {
26 this.Validate();
27 }
28 }
29 }
30
31
2 {
3 if (this._registeredControlThatRequireRaiseEvent != null)
4 {
5 this.RaisePostBackEvent(this._registeredControlThatRequireRaiseEvent, null);
6 }
7 else
8 {
9 string str = postData["__EVENTTARGET"];
10 bool flag = !string.IsNullOrEmpty(str);
11 if (flag || (this.AutoPostBackControl != null))
12 {
13 Control control = null;
14 if (flag)
15 {
16 control = this.FindControl(str);
17 }
18 if ((control != null) && (control.PostBackEventHandler != null))
19 {
20 string eventArgument = postData["__EVENTARGUMENT"];
21 this.RaisePostBackEvent(control.PostBackEventHandler, eventArgument);
22 }
23 }
24 else
25 {
26 this.Validate();
27 }
28 }
29 }
30
31