Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement ASP.NET Asynchronous callback

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report