国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

ASP.NET2.0中控件的簡單異步回調(diào)_.Net教程

2024-07-10 12:50:37
字體:
供稿:網(wǎng)友

推薦:asp.net客戶端回調(diào)功能的實(shí)現(xiàn)機(jī)制
實(shí)現(xiàn)的過程基本上就是:讓要實(shí)現(xiàn)客戶端回調(diào)功能的頁面或者空間實(shí)現(xiàn)System.Web.UI.ICallbackEventHandler的接口,即2個(gè)方法:void RaiseCallbackEvent(string eventArgument),這個(gè)是當(dāng)客戶端觸

雖然已經(jīng)有了ASP.NET AJAX了,最近學(xué)習(xí)ASP.NET控件的時(shí)候,逐步理解了原始的控件異步回調(diào)(代碼取自《ASP.NET 2.0 高級編程》):
首先,在Render事件中添加好一個(gè)事件

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) "}");
//向服務(wù)器發(fā)送請求,由服務(wù)器端生成回調(diào)的客戶端腳本。
}
也就是在服務(wù)器端生成客戶端代碼,注意最后一個(gè)方法GetCallbackEventReference,我理解的是在服務(wù)器端捕捉了客戶端的請求之后,生成相應(yīng)的客戶端腳本,在服務(wù)器端回調(diào)的時(shí)候,客戶端決定用什么函數(shù)處理回調(diào)和錯(cuò)誤。

服務(wù)器端實(shí)現(xiàn)接口的一個(gè)方法,也就是接收到客戶端的請求之后,由服務(wù)器端先處理,然后再把結(jié)果和相應(yīng)代碼發(fā)回客戶端。

#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文件中寫好相應(yīng)的回調(diào)處理函數(shù)即可:
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用MD5和SHA1加密的幾種方法
首先簡單介紹一下MD5和SHA1: MD5的全稱是Message-Digest Algorithm 5(信息-摘要算法),在90年代初由Mit Laboratory for Computer Science和Rsa data security inc的Ronald l. rivest開發(fā)出

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 麟游县| 门头沟区| 罗平县| 广西| 江油市| 汕头市| 土默特右旗| 建昌县| 谢通门县| 华容县| 阿坝| 泽普县| 皋兰县| 洛浦县| 安新县| 平安县| 呈贡县| 浦江县| 汽车| 西充县| 叙永县| 平顺县| 原阳县| 双峰县| 三穗县| 泽普县| 汶川县| 普格县| 东阿县| 宿迁市| 古交市| 土默特右旗| 余姚市| 黎平县| 漳州市| 开阳县| 嘉祥县| 彩票| 和龙市| 广安市| 林周县|