用最简单的方法实现Ajax
Aspx文件:
public partial class Test : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected string callbackScript
{
get
{
ClientScriptManager cm = Page.ClientScript;
//return this.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
return cm.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
}
}
private string arg = "";
protected void Page_Load(object sender, EventArgs e)
{
}
public void RaiseCallbackEvent(string eventArgument)
{
arg = eventArgument + Request["a"];
for (int i = 0; i < Request.Params.Count; i++)
{
arg += "Name:" + Request.Params.GetKey(i) +":Value:<span style=\"color:#ff0000\">" + Request.Params[i] + "</span><br/>";
}
}
public string GetCallbackResult()
{
return "客户端在[" + DateTime.Now.ToString() + "]传送来 [" + arg + "].";
}
}
这里主要是用到了Asp.net自带的 System.Web.UI.ICallbackEventHandler接口。
实现的这个接口的控件(包括页面)都可以以异步的方式用服务器发送数据。还可以设置一个回调函数。这个方法有两个局限:函数的返回类型只能是字符串类型。方法只有一个参数(可以通过分割字符串实现多个参数,但不安全。)
<script type="text/javascript">
function callToServer()
{
var param = document.getElementById("txtMessage").value + "&pa"+document.getElementById("a").value;
var context = "";
document.getElementById("a").value = document.getElementById("a").value;
// WebForm_InitCallback();
<% = callbackScript %>
}
function handleResultFromServer(result)
{
document.getElementById("txtResult").innerHTML = result;
}
</script>
<form id="form1" runat="server">
<div><INPUT id="txtMessage" />
a<input id="a" name="a" />
<INPUT onclick="callToServer();" type="button" value="Call to Server" />
<br />
<span style="color:#ff0000"></span>
Result :
<div id="txtResult"></div>
</div>
</form>
Aspx.cs文件:function callToServer()
{
var param = document.getElementById("txtMessage").value + "&pa"+document.getElementById("a").value;
var context = "";
document.getElementById("a").value = document.getElementById("a").value;
// WebForm_InitCallback();
<% = callbackScript %>
}
function handleResultFromServer(result)
{
document.getElementById("txtResult").innerHTML = result;
}
</script>
<form id="form1" runat="server">
<div><INPUT id="txtMessage" />
a<input id="a" name="a" />
<INPUT onclick="callToServer();" type="button" value="Call to Server" />
<br />
<span style="color:#ff0000"></span>
Result :
<div id="txtResult"></div>
</div>
</form>
public partial class Test : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected string callbackScript
{
get
{
ClientScriptManager cm = Page.ClientScript;
//return this.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
return cm.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
}
}
private string arg = "";
protected void Page_Load(object sender, EventArgs e)
{
}
public void RaiseCallbackEvent(string eventArgument)
{
arg = eventArgument + Request["a"];
for (int i = 0; i < Request.Params.Count; i++)
{
arg += "Name:" + Request.Params.GetKey(i) +":Value:<span style=\"color:#ff0000\">" + Request.Params[i] + "</span><br/>";
}
}
public string GetCallbackResult()
{
return "客户端在[" + DateTime.Now.ToString() + "]传送来 [" + arg + "].";
}
}
实现的这个接口的控件(包括页面)都可以以异步的方式用服务器发送数据。还可以设置一个回调函数。这个方法有两个局限:函数的返回类型只能是字符串类型。方法只有一个参数(可以通过分割字符串实现多个参数,但不安全。)