在ASP.NET 2.0里面,我们可以轻松的来做到这点了。服务器端任何实现了System.Web.UI.ICallbackEventHandler接口的控件,都可以通过RaiseCallbackEvent()方法来处理从页面上的JS脚本传递过来的请求和数据,处理后,再将结果传回给页面。这项能力的底层仍然是XMLHTTP。 下面是一个简单的演示:
在页面上,我们放上两个文本框和一个按钮:
<INPUT id="txtMessage">
<INPUT onclick="callToServer();" type="button" value="Call to Server">
Result : <INPUT id="txtResult" >
当点击按钮的时候,将调用JS脚本方法callToServer(),JS脚本如下:
<INPUT onclick="callToServer();" type="button" value="Call to Server">
Result : <INPUT id="txtResult" >
function callToServer()
{
var param = document.getElementById("txtUsername").value;
var context = "";
<% = ClientScript %>
}
function handleResultFromServer(result, context)
{
document.getElementById("txtResult").value = result;
}
handleResultFromServer()方法则负责将从服务器传回的数据写到txtResult这个文本框里面。 {
var param = document.getElementById("txtUsername").value;
var context = "";
<% = ClientScript %>
}
function handleResultFromServer(result, context)
{
document.getElementById("txtResult").value = result;
}
再看看服务器端的代码:
public partial class Default_aspx : System.Web.UI.ICallbackEventHandler
{
private String ClientScript
{
get
{
return this.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
}
}
public string RaiseCallbackEvent(string eventArgument)
{
return "客户端在[" + DateTime.Now.ToString() + "]传送来 [" + eventArgument + "].";
}
}
我们让页面直接实现ICallbackEventHandler接口,然后接口定义的RaiseCallbackEvent()方法中将服务器的时间和传来的数据一起返回回去。 {
private String ClientScript
{
get
{
return this.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
}
}
public string RaiseCallbackEvent(string eventArgument)
{
return "客户端在[" + DateTime.Now.ToString() + "]传送来 [" + eventArgument + "].";
}
}
ClientScript属性的作用是,它调用了页面的GetCallbackEventReference()方法,获得了让客户端有能力调用服务器端方法的JS脚本,并输出到页面的callToServer()方法中,这样,点击页面按钮时,就开始执行页面上包含了调用服务器方法的的callToServer()方法。
注意GetCallbackEventReference()方法的参数,在参数中,我们定义了客户端的哪个变量包含了要传递给服务器,服务器方法执行后,调用客户端的哪个方法等信息。实现步骤:首先,调用Page.GetCallbackEvenReference以获取对某个特定函数(可以从客户端脚本中调用该函数,以执行到服务器的XM—HTTP回调)的应用。asp.net提供了该函数的名称和实现。其次,在客户端脚本中编写一个将在回调时调用的方法。方法名称是传递给GetCallbackEventReference的参数之一。第三,在页面中实现ICallbackEventHandler接口,该接口包含一个方法—RaiseCallbackEvent,当回调发生时,该方法将在服务器段调用,RaiseCallbackEvent所返回的字符串讲被返回到第二步所述的方法
下面是一个例子:
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
<html>
<body>
<h1>Please Register</h1>
<hr>
<form runat="server">
<table>
<tr>
<td>First Name</td>
<td><asp:TextBox ID="FirstName" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Last Name</td>
<td><asp:TextBox ID="LastName" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Address 1</td>
<td><asp:TextBox ID="Address1" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Address 2</td>
<td><asp:TextBox ID="Address2" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>City</td>
<td><asp:TextBox ID="City" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>State</td>
<td><asp:TextBox ID="State" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Zip Code</td>
<td><asp:TextBox ID="Zip" RunAt="server" /></td>
<td><asp:Button ID="AutofillButton" Text="Autofill"
RunAt="server" /></td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">
// Function called when callback returns
function __onCallbackCompleted (result, context)
{
// Display the string returned by the server's RaiseCallbackEvent
// method in the input field named "City"
document.getElementById ('City').value = result;
}
</script>
<script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e)
{
// Get callback event reference (e.g., "__doCallback ()")
string cbref = GetCallbackEventReference (this,
"document.getElementById ('Zip').value",
"__onCallbackCompleted", "null", "null");
// Wire the callback event reference to the Autofill button with
// an onclick attribute (and add "return false" to event reference
// to prevent a postback from occurring)
AutofillButton.Attributes.Add ("onclick",
cbref + "; return false;");
}
// Server-side callback event handler
string ICallbackEventHandler.RaiseCallbackEvent (string arg)
{
if (arg.StartsWith ("378"))
return "Oak Ridge";
else if (arg.StartsWith ("379"))
return "Knoxville";
else
return "Unknown";
}
</script>
<html>
<body>
<h1>Please Register</h1>
<hr>
<form runat="server">
<table>
<tr>
<td>First Name</td>
<td><asp:TextBox ID="FirstName" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Last Name</td>
<td><asp:TextBox ID="LastName" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Address 1</td>
<td><asp:TextBox ID="Address1" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Address 2</td>
<td><asp:TextBox ID="Address2" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>City</td>
<td><asp:TextBox ID="City" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>State</td>
<td><asp:TextBox ID="State" RunAt="server" /></td>
<td></td>
</tr>
<tr>
<td>Zip Code</td>
<td><asp:TextBox ID="Zip" RunAt="server" /></td>
<td><asp:Button ID="AutofillButton" Text="Autofill"
RunAt="server" /></td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript">
// Function called when callback returns
function __onCallbackCompleted (result, context)
{
// Display the string returned by the server's RaiseCallbackEvent
// method in the input field named "City"
document.getElementById ('City').value = result;
}
</script>
<script language="C#" runat="server">
void Page_Load (Object sender, EventArgs e)
{
// Get callback event reference (e.g., "__doCallback ()")
string cbref = GetCallbackEventReference (this,
"document.getElementById ('Zip').value",
"__onCallbackCompleted", "null", "null");
// Wire the callback event reference to the Autofill button with
// an onclick attribute (and add "return false" to event reference
// to prevent a postback from occurring)
AutofillButton.Attributes.Add ("onclick",
cbref + "; return false;");
}
// Server-side callback event handler
string ICallbackEventHandler.RaiseCallbackEvent (string arg)
{
if (arg.StartsWith ("378"))
return "Oak Ridge";
else if (arg.StartsWith ("379"))
return "Knoxville";
else
return "Unknown";
}
</script>