Mike Nichols - Son of Nun Technology

Update Panel Request Handler Control

I am sure there is something already builtin to the UpdatePanel for this (?) but I couldn't find it and wanted to get it done.

I have a number of controls on page that need to be refreshed after callbacks from UpdatePanel. Namely, I am using Peter Blum's validators and wanted to validate a group of controls after a callback. Rather than writing client script over and over, I create a UpdatePanelRequestHandler control that you can drop on your page and it'll create the add_endRequest or add_beginRequest client scripts for you can register them as Startup scripts.

Just tell the control the script to run on either begin or end callback and it'll render it to your page. I am just pasting the control here...

[UPDATE] I have created a much more flexible control exposing these client side events as a server control to easily be able to hook client side scripts before/after callbacks . You can get the code and tests at code.google.com/p/son-of-nun/


 

    [NonVisualControl, Designer(typeof(UpdatePanelRequestHandlerControlDesigner))]
[ParseChildren(true)]
[PersistChildren(false)]
[DefaultProperty("ScriptToRunOnEndRequest")]
public class UpdatePanelRequestHandler : Control
{
private string _beginRequestFunctionName = "begin_{0}";
private string _endRequestFunctionName = "end_{0}";
private string _scriptToRunOnEndRequest = string.Empty;
private string _scriptToRunOnBeginRequest = string.Empty;
public const string end_Request = "add_endRequest";
public const string begin_Request = "add_beginRequest";
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
_endRequestFunctionName = string.Format(_beginRequestFunctionName, this.ClientID);
_beginRequestFunctionName = string.Format(_endRequestFunctionName, this.ClientID);
}

public string BeginRequestFunctionName
{
get { return _beginRequestFunctionName; }
set { _beginRequestFunctionName = value; }
}

public string EndRequestFunctionName
{
get { return _endRequestFunctionName; }
set { _endRequestFunctionName = value; }
}

public string ScriptToRunOnBeginRequest
{
get { return _scriptToRunOnBeginRequest; }
set { _scriptToRunOnBeginRequest = value; }
}

public string ScriptToRunOnEndRequest
{
get { return _scriptToRunOnEndRequest; }
set { _scriptToRunOnEndRequest = value; }
}
private string CreateRequestScript(bool isBeginRequest,string requestFnName,string scriptToRun)
{
string handlerScriptName = isBeginRequest ? begin_Request : end_Request;

StringBuilder script = new StringBuilder();
script.AppendFormat("Sys.WebForms.PageRequestManager.getInstance().{0}({1});function {1}",handlerScriptName,requestFnName);
script.Append("(sender,e){if (e.get_error() == null){");
script.Append(scriptToRun).Append(";}}");
return script.ToString();
}
public string EndRequestScript
{
get
{
if (string.IsNullOrEmpty(ScriptToRunOnEndRequest))
return string.Empty;
return CreateRequestScript(false, EndRequestFunctionName, ScriptToRunOnEndRequest);
}
}
public string BeginRequestScript
{
get
{
if (string.IsNullOrEmpty(ScriptToRunOnBeginRequest))
return string.Empty;
return CreateRequestScript(true, BeginRequestFunctionName, ScriptToRunOnBeginRequest);
}
}
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(e);
StringBuilder scripts = new StringBuilder();
scripts.Append(BeginRequestScript).Append(EndRequestScript);

if (scripts.Length > 0)
{
StringBuilder script = new StringBuilder();
script.Append("<script type='text/javascript'>");
script.Append(scripts.ToString());
script.Append("</script>");
Page.ClientScript.RegisterStartupScript(this.GetType(), this.ClientID + "_requestHandler",
script.ToString());
}
}

}
internal class UpdatePanelRequestHandlerControlDesigner : ControlDesigner
{
public override string GetDesignTimeHtml()
{
return base.CreatePlaceHolderDesignTimeHtml("");
}
}