1: public class CallbackControl : Control, ICallbackEventHandler
2: {
3: #region Public constructor
4: public CallbackControl()
5: {
6: this.SendAllData = false;
7: this.Async = true;
8: }
9: #endregion
10:
11: #region Public properties and events
12: public event EventHandler<CallbackEventArgs> Callback;
13:
14: [DefaultValue(true)]
15: public Boolean Async
16: {
17: get;
18: set;
19: }
20:
21: [DefaultValue(false)]
22: public Boolean SendAllData
23: {
24: get;
25: set;
26: }
27:
28: #endregion
29:
30: #region Protected override methods
31:
32: protected override void Render(HtmlTextWriter writer)
33: {
34: writer.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
35: writer.RenderBeginTag(HtmlTextWriterTag.Span);
36:
37: base.Render(writer);
38:
39: writer.RenderEndTag();
40: }
41:
42: protected override void OnInit(EventArgs e)
43: {
44: String reference = this.Page.ClientScript.GetCallbackEventReference(this, "arg", "onCallbackSuccess", "context", "onCallbackError", this.Async);
45: String script = String.Concat("\ndocument.getElementById('", this.ClientID, "').callback = function(arg, context, onCallbackSuccess, onCallbackError){", ((this.SendAllData == true) ? "__theFormPostCollection.length = 0; __theFormPostData = ''; WebForm_InitCallback(); " : String.Empty), reference, ";};\n");
46:
47: this.Page.ClientScript.RegisterStartupScript(this.GetType(), String.Concat("callback", this.ClientID), script, true);
48:
49: base.OnInit(e);
50: }
51:
52: #endregion
53:
54: #region Protected virtual methods
55: protected virtual void OnCallback(CallbackEventArgs args)
56: {
57: EventHandler<CallbackEventArgs> handler = this.Callback;
58:
59: if (handler != null)
60: {
61: handler(this, args);
62: }
63: }
64:
65: #endregion
66:
67: #region ICallbackEventHandler Members
68:
69: String ICallbackEventHandler.GetCallbackResult()
70: {
71: CallbackEventArgs args = new CallbackEventArgs(this.Context.Items["Data"] as String);
72:
73: this.OnCallback(args);
74:
75: return (args.Result);
76: }
77:
78: void ICallbackEventHandler.RaiseCallbackEvent(String eventArgument)
79: {
80: this.Context.Items["Data"] = eventArgument;
81: }
82:
83: #endregion
84: }