Can anyone help me to describe me following code line by line.
protected override void OnPreRender(EventArgs e)
{
String eventRef = Page.ClientScript.GetCallbackEventReference(this, "", "", "");
// Register include file
String includeScript = Page.ResolveClientUrl("~/ClientScripts/AjaxValidator.js");
Page.ClientScript.RegisterClientScriptInclude("AjaxValidator", includeScript);
// Register startup script
String startupScript = String.Format("document.getElementById('{0}').evaluationfunction = 'AjaxValidatorEvaluateIsValid';", this.ClientID);
Page.ClientScript.RegisterStartupScript(this.GetType(), "AjaxValidator", startupScript, true);
base.OnPreRender(e);
}
The
GetCallbackEventReferencemethod returns a string with the JavaScriptWebForm_DoCallbackfunction, this function performs out-of-band callbacks to the server. It also renders a script tag to the client with it’s source attribute set to WebResource.axd. WebResource.axd is an HTTP Handler that enables downloading of resources that are embedded in an assembly. The resource contains theWebForm_DoCallbackfunction. The eventRef string with theWebForm_DoCallbackfunction is never injected into the client and the parameters are all empty so im assuming this line is used just to output the WebResource.axd to the page.String includeScript = Page.ResolveClientUrl("~/ClientScripts/AjaxValidator.js");Page.ClientScript.RegisterClientScriptInclude("AjaxValidator", includeScript);The first line gets a relative path to the external JavaScript file ‘AjaxValidator.js’. The second line injects a client script tag with the source set to the path of the external JavaScript file returned by ResolveClientUrl.
String startupScript = String.Format("document.getElementById('{0}').evaluationfunction= 'AjaxValidatorEvaluateIsValid';", this.ClientID);Page.ClientScript.RegisterStartupScript(this.GetType(), "AjaxValidator", startupScript);The first of the last two lines creates JavaScript code to be rendered to the client. The script block added by the
RegisterStartupScriptmethod executes when the page finishes loading but before the page’s OnLoad event is raised. The ‘evaluationfunction’ is set to the to the method to be called when the page validates on the client, it’s called by theValidatorValidatemethod located in the WebUIValidation.js script (the WebResource.axd is used to retrieve this file also). This line doesn’t make much sense out of context. I’m assuming the PreRender event is part of a custom validator control that inherits from the BaseValidator class.