欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net回调技术Callback学习笔记

程序员文章站 2024-02-22 07:58:10
.aspx: <%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs...

.aspx:

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>无标题页</title> 

<script type="text/javascript"> 

//向服务器传递参数 
function dosearch(){ 
var firstname=document.getelementbyid("textbox1").value; 
callserver(firstname,""); 
} 

//得到服务器的数据 
function receiveserverdata(txtuserinfo){ 
results.innerhtml=txtuserinfo; 
} 

//设置每1秒执行一次 
setinterval("dosearch()",1000); 
</script> 

</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
姓名:<asp:textbox id="textbox1" runat="server"></asp:textbox> 
<br /> 
<span id="results" style=" width:500px;"></span> 
</div> 
</form> 
</body> 
</html>
[/code]
.aspx.cs
[code]
using system; 
using system.collections; 
using system.configuration; 
using system.data; 
using system.web; 
using system.web.security; 
using system.web.ui; 
using system.web.ui.htmlcontrols; 
using system.web.ui.webcontrols; 
using system.web.ui.webcontrols.webparts; 
using system.data.sqlclient; 

public partial class _default : system.web.ui.page, icallbackeventhandler 
{ 
protected string txtuserinfo; 


protected void page_load(object sender, eventargs e) 
{ 
//获取一个对客户端函数的引用 
string cbreference = page.clientscript.getcallbackeventreference(this, "arg", "receiveserverdata", "context"); 
//动态注册回调函数 
string callbackscript = "function callserver(arg,context)" + "{" + cbreference + "};"; 
//引发callbackscript 
page.clientscript.registerstartupscript(this.gettype(), "callserver", callbackscript, true); 
} 

//引发callback事件处理 
public void raisecallbackevent(string txtfirstname) 
{ 
if (txtfirstname != null) 
{ 
string connstring = system.configuration.configurationmanager.connectionstrings["sqlserver2008"].tostring(); 

sqlconnection conn = new sqlconnection(connstring); 

conn.open(); 

sqlcommand comm = new sqlcommand("select * from zzx where [name]=@name", conn); 

comm.parameters.add("@name", sqldbtype.varchar).value = txtfirstname; 

sqldatareader reader = comm.executereader(commandbehavior.closeconnection); 
if (reader.read()) 
{ 
txtuserinfo = "员工编号:" + reader["id"].tostring() + "<br>"; 
txtuserinfo += "员工姓名:" + reader["name"].tostring() + "<br>"; 
txtuserinfo += "地址:" + reader["address"].tostring() + "<br>"; 
txtuserinfo += "服务器查询时间:" + datetime.now.tostring(); 
} 
else 
{ 
if (string.isnullorempty(txtfirstname)) 
{ 
txtuserinfo = "请输入姓名"; 
} 
else 
{ 
txtuserinfo = "查无此人"; 
} 
} 

comm.dispose(); 
reader.dispose(); 
conn.dispose(); 
} 
} 

//得到回调的结果,返回给客户端 
public string getcallbackresult() 
{ 
return txtuserinfo; 
} 


}

简化版(偷懒一下):

<%@ page language="c#" autoeventwireup="true" codefile="default.aspx.cs" inherits="_default" %> 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>无标题页</title> 

<script type="text/javascript"> 
function oncallback(txtuserinfo,context){ 
results.innerhtml=txtuserinfo; 
} 
</script> 

</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
姓名:<asp:textbox id="textbox1" runat="server"></asp:textbox> 
<input id="button2" type="button" value="button" 
onclick="<%=page.clientscript.getcallbackeventreference(this, "document.getelementbyid('textbox1').value", "oncallback",null)%>" /> 
<br /> 
<span id="results" style="pink; width: 500;"></span> 
</div> 
</form> 
</body> 
</html>
.aspx.cs

using system; 
using system.collections; 
using system.configuration; 
using system.data; 
using system.web; 
using system.web.security; 
using system.web.ui; 
using system.web.ui.htmlcontrols; 
using system.web.ui.webcontrols; 
using system.web.ui.webcontrols.webparts; 
using system.data.sqlclient; 
using system.text; 
public partial class _default : system.web.ui.page, icallbackeventhandler 
{ 
protected stringbuilder txtuserinfo; 

protected void page_load(object sender, eventargs e) 
{ 

} 

public string getcallbackresult() 
{ 
return txtuserinfo.tostring(); 
} 

public void raisecallbackevent(string txtfirstname) 
{ 
txtuserinfo = new stringbuilder(); 
string connstring = configurationmanager.connectionstrings["sqlserver2008"].tostring(); 
sqlconnection conn = new sqlconnection(connstring); 
conn.open(); 
sqlcommand comm = new sqlcommand("select * from zzx where [name]=@name", conn); 
comm.parameters.add("@name", sqldbtype.varchar).value = txtfirstname; 
sqldatareader reader = comm.executereader(commandbehavior.closeconnection); 
if (reader.read()) 
{ 
txtuserinfo.append("员工编号:" + reader["id"].tostring() + "<br>"); 
txtuserinfo.append("员工姓名:" + reader["name"].tostring() + "<br>"); 
txtuserinfo.append("地址:" + reader["address"].tostring() + "<br>"); 
txtuserinfo.append("查询时间:" + datetime.now.tostring()); 
} 
else 
{ 
if (txtfirstname == string.empty) 
{ 
txtuserinfo.append("请输入姓名"); 
} 
else 
{ 
txtuserinfo.append("查无此人"); 
} 
reader.dispose(); 
comm.dispose(); 
conn.dispose(); 
}
} 
}

示例3:

<%@ page language="c#" autoeventwireup="true" codefile="default3.aspx.cs" inherits="default3" %> 

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head runat="server"> 
<title>无标题页</title> 
<script type="text/javascript"> 
//客户端执行的方法 
//下面的方法是接收并处理服务器方法返回的结果 
function success(args,context){ 
message.innerhtml=args; 
} 

//下面的方式是当接收服务器方法处理的结果发生异常时调用的方法 
function error(){ 
message.innerhtml="发生了异常!"; 
} 
</script> 
</head> 
<body> 
<form id="form1" runat="server"> 
<div> 
用户名:<input type="text" id="txtusername" onblur="callservermethod(txtusername.value,null)" /> 
<span id="message"></span> 
<br /> 
密码:<input type="password" size="10" maxlength="20" id="txtpwd" /> 
</div> 
</form> 
</body> 
</html>
[code]
public partial class default3 : system.web.ui.page,icallbackeventhandler //实现icallbackeventhandler接口 
{ 

string result = string.empty; 

protected void page_load(object sender, eventargs e) 
{ 
//获取当前页的clientscriptmanager的引用 
clientscriptmanager csm = page.clientscript; 
/*获取回调的引用.会在客户端生成webform_docallback方法, 
* 调用它来达到异步调用.这个方法是微软写的方法,会被发送 
到客户端*/ 
/*注意这里的"success"和error两个字符串分别是客户端代码中 
*定义的两个javascript函数*/ 
//下面的方法最后一个参数的意义:true表示执行异步回调,false标志执行同步回调 
string reference = csm.getcallbackeventreference(this, "args", "success", "", "error", true); 
string callbackscript = "function callservermethod(args,context){\n"+ 
reference+";\n }"; 
//向当前页面注册javascript脚本代码 
csm.registerclientscriptblock(this.gettype(), "callservermethod",callbackscript,true); 
} 

#region icallbackeventhandler 成员 

/// <summary> 
/// 返回回调方法执行结果的方法 
/// </summary> 
public string getcallbackresult() 
{ 
return result; 
} 

/// <summary> 
/// 在服务器端运行回调方法 
/// </summary> 
public void raisecallbackevent(string eventargument) 
{ 
if (eventargument.tolower().indexof("admin")!=-1) 
{ 
result =eventargument+ "不能作为用户注册."; 
} 
else 
{ 
result = eventargument + "可以注册."; 
} 
} 

#endregion 
}