Client Script and get call back event reference.
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);
}
String eventRef = Page.ClientScript.GetCallbackEventReference(this, "", "", "");
The GetCallbackEventReference
method returns a string with the JavaScript WebForm_DoCallback
function, 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 the WebForm_DoCallback
function. The eventRef string with the WebForm_DoCallback
function 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 RegisterStartupScript
method 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 the ValidatorValidate
method 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.
精彩评论