Simple asynchronous callback for controls in ASP.net 2.0

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.