Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement ASP.NET Asynchronous callback

Shulou Source: shulou.com Published: 2022-06-02 07:59:12 09月13日 Update

This article focuses on "how to achieve ASP.NET asynchronous callback", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve ASP.NET asynchronous callback"!

ASP.NET asynchronous callback example:

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 ();")

In the OnPreRender event of the ASP.NET asynchronous callback instance, add the following code:

Protected override void OnPreRe nder (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) +"} "); / / sends a request to the server, and the client script for callback is generated by the server. }

That is, the client code is generated on the server side. Note * A method GetCallbackEventReference. What I understand is that after the server captures the client request, the corresponding client script is generated. During the server callback, the client decides what function to use to handle callbacks and errors.

A method of implementing the interface on the server side of the ASP.NET asynchronous callback instance, that is, after receiving the request from the client, the server processes it first, and then sends the result and the corresponding code 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

For an ASP.NET asynchronous callback instance, write the corresponding callback handler in the jscript.js file:

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") } at this point, I believe you have a deeper understanding of "how to implement ASP.NET asynchronous callback". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Client server service client instance event code method processing generation that is content function side script learning practical deeper important interest Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL macOS Huawei NVidia Apple