这些天研究AJAX, 没事翻看msdn的文章, 偶然发现一篇<<ASP.NET 中的自定义脚本回调>>的文章, 顿时眼睛一亮, 心像这不实现我想要得AJAX局部页面更新功能了么! 但这篇文章字太多, 看着费劲, 我就试着找一些代码例子, 结果找到了一些代码, 但都是beta 版http://www.dotnetjunkies.com/Tutorial/E80EC96F-1C32-4855-85AE-9E30EECF13D7.dcik, 在正式版中编译不过, 后来参考msdn, 终于让他通过, 简单易行.
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
.aspx code
1
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
2
<%@ Implements Interface="System.Web.UI.ICallbackEventHandler" %>
3
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>Untitled Page</title>
8
<script type="text/javascript">
9![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
function GetServerTime()
11![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
12
var message = '';
13
var context = '';
14
15
<%=sCallBackFunctionInvocation%>
16
}
17
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function ShowServerTime(timeMessage, context)
{
19
alert('The time on the server is:\n' + document.getElementById("Label1").title +timeMessage);
20
document.getElementById("Label1").innerHTML = timeMessage;
21
}
22
23![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
function OnError(message, context)
{
24
alert('An unhandled exception has occurred:\n' + message);
25
}
26
</script>
27
</head>
28
<body>
29
<form id="form1" runat="server">
30
<div>
31
<input type="button" value="Get Server Time" onclick="GetServerTime();" />
32
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
33
<br />
34
<span id="Label1">time
</span></div>
35
36
</form>
37
</body>
38
</html>
39![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
aspx.cs code
1
using System;
2
using System.Web;
3
using System.Web.Security;
4
using System.Web.UI;
5
using System.Web.UI.WebControls;
6
using System.Web.UI.HtmlControls;
7![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
public partial class Default2 : System.Web.UI.Page, ICallbackEventHandler
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
10
public string sCallBackFunctionInvocation;
11
protected void Page_Load(object sender, EventArgs e)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
ClientScriptManager cs = Page.ClientScript;
14
sCallBackFunctionInvocation = cs.GetCallbackEventReference(this, "message", "ShowServerTime", "context", true);
15
}
16
17
public void RaiseCallbackEvent(String eventArgument)
18![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
sCallBackFunctionInvocation = eventArgument + DateTime.Now.ToString();
20
}
21
public string GetCallbackResult()
22![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
23
return sCallBackFunctionInvocation;
24
}
25
}
26![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
如果不想去别的网站 截取内容, 再加点javascript不就实现了传说中的ajax功能了么? 还去研究那么赋值ajax libraries干嘛呢, 不过rico的drag-drop功能,还是很眩的, 可以用一下. 省下的就是等MS的ajax出来再研究就行了.
可恨live writer 不支持插入code. 每次贴code都很费劲
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步