Although there is already asp.net Ajax, the most recent learning asp.net control, the original control of the asynchronous callback (code from "ASP.net 2.0 Advanced Programming") is gradually understood:
First, add an event to the render event
protected override void RenderContents(HtmlTextWriter output)
{
output.RenderBeginTag(HtmlTextWriterTag.Div);
output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
output.AddAttribute(HtmlTextWriterAttribute.Name, this.ClientID);
output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
output.AddAttribute("OnBlur", "ClientCallback();");
this.AddAttributesToRender(output);
output.RenderBeginTag(HtmlTextWriterTag.Input);
output.RenderEndTag();
output.RenderEndTag();
}
The most important thing here is output. AddAttribute ("OnBlur", "Clientcallback ();");
Then, in the OnPreRender event, add the following code:protected override void OnPreRender(EventArgs e)
{
//Page.ClientScript.RegisterClientScriptInclude("UtilityFunctions", "JScript.js");
Page.ClientScript.RegisterStartupScript(typeof(Page), "ControlFocus", "document.getElementById('" + this.ClientID + "').focus();", true);
Page.ClientScript.RegisterStartupScript(typeof(Page),"ClientCallback","function ClientCallback() {"+"args=document.getElementById('"+this.ClientID+"').value;"+Page.ClientScript.GetCallbackEventReference(this,"args","CallbackHandler",null,"ErrorHandler",true)+"}");
//向服务器发送请求,由服务器端生成回调的客户端脚本。
}
That is, generate client code on the server side, note that the last method GetCallbackEventReference, I understand that after the server to capture the client's request, generate the corresponding client script, when the server-side callback, The client decides what functions to use to handle callbacks and errors.
Server-side implementation of the interface of a method, that is, after receiving the client's request, the server-side first processing, and then the results and the corresponding code to send back to the client.
#region ICallbackEventHandler Members
public string RaiseCallbackEvent(string eventArgument)
{
int result;
if (!Int32.TryParse(eventArgument, out result))
throw new Exception("The method is not implemented.");
return "Valid Data";
}
#endregion 最后,在jscript.js文件中写好相应的回调处理函数即可:
var args;
var ctx;
function ValidateText(ctl)
{
if(ctl.value=='')
{
alert("Please enter a value");
ctl.focus();
}
}
function CallbackHandler(args,ctx)
{
alert("The data is valid");
}
function ErrorHandler(args,ctx)
{
alert("The data is not a number");
}