温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

ASP.NET异步回调怎么实现

发布时间:2021-12-06 09:56:26 来源:亿速云 阅读:195 作者:iii 栏目:编程语言

本篇内容主要讲解“ASP.NET异步回调怎么实现”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“ASP.NET异步回调怎么实现”吧!

ASP.NET异步回调实例:

首先,在Render事件中添加好一个事件

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

这里最重要的就是output.AddAttribute("OnBlur","ClientCallback();");

ASP.NET异步回调实例之在OnPreRender事件中,添加如下代码:

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)+"}");   //向服务器发送请求,由服务器端生成回调的客户端脚本。   }

也就是在服务器端生成客户端代码,注意***一个方法GetCallbackEventReference,我理解的是在服务器端捕捉了客户端的请求之后,生成相应的客户端脚本,在服务器端回调的时候,客户端决定用什么函数处理回调和错误。

ASP.NET异步回调实例之服务器端实现接口的一个方法,也就是接收到客户端的请求之后,由服务器端先处理,然后再把结果和相应代码发回客户端。

#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

ASP.NET异步回调实例之在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");   }

到此,相信大家对“ASP.NET异步回调怎么实现”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI