All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
JavaScript实现DropDownList(Select)三级联动无刷新_[Asp.Net教程]
.NET程序调用SSIS中的DTS包_.net资料_编程技术
添加数据集(DataSet)、数据表(DataTable)、行、列、主键和外键等操作示例代码_[Asp.Net教程]
ASP.NET实例:手把手教你如何扩展GridView之自带分页_[Asp.Net教程]
asp.net,2.0中使用OWC组件学习小结_[Asp.Net教程]
c#数据结构中的基本概念和术语
C#,中启动进程的三种方法_[Asp.Net教程]
ASP.Net常用功能整理——生成图片的缩略图_[Asp.Net教程]
真正的取真实IP地址及利弊_[Asp.Net教程]
W3C专业术语翻译对照表_[Html教程]
Reading number is top 10 pictures
A man's favorite things8
泳装名模阿尔贝特妮写真2
The world's top ten most beautiful railway station2
Take you to walk into the most true north Korea rural1
浴室里的美女
再发两张抽象画
美洲杯宝贝的雨中风情1
Terrorist smile the largest human history an explosion2
More attractive than sora aoi2
云南大理的美女
Download software ranking
Tram sex maniac 2 (H) rar bag6
jBuilder2006
jdk1.6 for windows
The cock of the Grosvenor LTD handsome
C++教程第四版
matrix1
Ashlynn Video1
Unix video tutorial3
星际争霸1.08硬盘免安装版
C++编程教程第三版
aaa published in(发表于) 2013/12/17 7:45:41 Edit(编辑)
ASP.NET2.0中控件的简单异步回调_.net资料_编程技术

ASP.NET2.0中控件的简单异步回调_.net资料_编程技术

ASP.NET2.0中控件的简单异步回调_.net资料_编程技术-你的首页-uuhomepage.com

虽然已经有了ASP.NET AJAX了,最近学习ASP.NET控件的时候,逐步理解了原始的控件异步回调(代码取自《ASP.NET 2.0 高级编程》):


首先,在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();");


然后在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,我理解的是在服务器端捕捉了客户端的请求之后,生成相应的客户端脚本,在服务器端回调的时候,客户端决定用什么函数处理回调和错误。


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


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





添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.