using System;
using System.Web.Script.Serialization;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
namespace WMB {
/// <summary>
/// Summary description for MyCallBackControl
/// </summary>
[ParseChildren(typeof(CallbackTrigger), ChildrenAsProperties = true, DefaultProperty = "Triggers")]
public class CallbackController : Control, ICallbackEventHandler {
/// <summary>
/// Initializes a new instance of the <see cref="CallbackController"/> class.
/// </summary>
public CallbackController() {
}
/// <summary>
/// Gets or sets the callback script.
/// </summary>
/// <value>The callback script.</value>
public string CallbackScript { get; set; }
private CallbackTriggerCollection _triggers;
[DefaultValue((string)null), PersistenceMode(PersistenceMode.InnerDefaultProperty), MergableProperty(false)]
public CallbackTriggerCollection Triggers {
get {
if (_triggers == null) {
_triggers = new CallbackTriggerCollection();
}
return _triggers;
}
}
/// <summary>
/// Returns a JSON representation of the object. Just for an ease of use.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string JsonSerialize(object value) {
return new JavaScriptSerializer().Serialize(value);
}
/// <summary>
/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
/// </summary>
/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
protected override void OnPreRender(EventArgs e) {
Type callbackControlerType = typeof(CallbackController);
if (!Page.ClientScript.IsClientScriptBlockRegistered(callbackControlerType, this.ClientID)) {
string clientScript = string.Format("function Trigger_{0}(a,c){{{1}}};\n",
this.ClientID,
Page.ClientScript.GetCallbackEventReference(this, "a", CallbackScript, "c"));
Page.ClientScript.RegisterClientScriptBlock(callbackControlerType,
this.ClientID,
clientScript,
true);
}
if(!Page.ClientScript.IsStartupScriptRegistered(callbackControlerType, this.ClientID)){
string startupScript = string.Empty;
foreach(CallbackTrigger trigger in Triggers){
startupScript += "\n" + trigger.GetTriggerScript(this);
}
Page.ClientScript.RegisterStartupScript(callbackControlerType, this.ClientID, startupScript, true);
}
base.OnPreRender(e);
}
/// <summary>
/// Occurs when the <see cref="E:OnCallback"/> method gets called. The <see cref="E:RaiseCallbackEvent"/> does so.
/// </summary>
public event EventHandler<CallbackEventArgs> Callback;
/// <summary>
/// Raises the <see cref="E:Callback"/> event.
/// </summary>
/// <param name="e">The <see cref="WMB.CallbackEventArgs"/> instance containing the event data.</param>
protected virtual void OnCallback(CallbackEventArgs e) {
EventHandler<CallbackEventArgs> temp = Callback;
if (temp != null) {
temp(this, e);
}
}
#region ICallbackEventHandler Members
private string result;
/// <summary>
/// Returns the results of a callback event that targets a control.
/// </summary>
/// <returns>The result of the callback.</returns>
public string GetCallbackResult() {
return result;
}
/// <summary>
/// Processes a callback event that targets a control.
/// </summary>
/// <param name="eventArgument">A string that represents an event argument to pass to the event handler.</param>
public void RaiseCallbackEvent(string eventArgument) {
CallbackEventArgs e = new CallbackEventArgs(eventArgument);
OnCallback(e);
result = e.Result;
}
#endregion
}
/// <summary>
///
/// </summary>
public class CallbackEventArgs : EventArgs {
/// <summary>
/// Initializes a new instance of the <see cref="CallbackEventArgs"/> class.
/// </summary>
/// <param name="argument">The argument.</param>
public CallbackEventArgs(string argument) {
this.Argument = argument;
}
/// <summary>
/// Gets or sets the result.
/// </summary>
/// <value>The result.</value>
public string Result { get; set; }
/// <summary>
/// Gets the argument.
/// </summary>
/// <value>The argument.</value>
public string Argument { get; private set; }
}
}