Custom Script Callbacks in ASP.NET 2.0 without refresh whole Page
2007-08-02 10:26
585 查看
这些天研究AJAX, 没事翻看msdn的文章, 偶然发现一篇<<ASP.NET 中的自定义脚本回调>>的文章, 顿时眼睛一亮, 心像这不实现我想要得AJAX局部页面更新功能了么! 但这篇文章字太多, 看着费劲, 我就试着找一些代码例子, 结果找到了一些代码, 但都是beta 版http://www.dotnetjunkies.com/Tutorial/E80EC96F-1C32-4855-85AE-9E30EECF13D7.dcik, 在正式版中编译不过, 后来参考msdn, 终于让他通过, 简单易行.
.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
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>Untitled Page</title>
8
<script type="text/javascript">
9
10
function GetServerTime()
11
{
12
var message = '';
13
var context = '';
14
15
<%=sCallBackFunctionInvocation%>
16
}
17
18
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
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
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
8
public partial class Default2 : System.Web.UI.Page, ICallbackEventHandler
9
{
10
public string sCallBackFunctionInvocation;
11
protected void Page_Load(object sender, EventArgs e)
12
{
13
ClientScriptManager cs = Page.ClientScript;
14
sCallBackFunctionInvocation = cs.GetCallbackEventReference(this, "message", "ShowServerTime", "context", true);
15
}
16
17
public void RaiseCallbackEvent(String eventArgument)
18
{
19
sCallBackFunctionInvocation = eventArgument + DateTime.Now.ToString();
20
}
21
public string GetCallbackResult()
22
{
23
return sCallBackFunctionInvocation;
24
}
25
}
26
如果不想去别的网站 截取内容, 再加点javascript不就实现了传说中的ajax功能了么? 还去研究那么赋值ajax libraries干嘛呢, 不过rico的drag-drop功能,还是很眩的, 可以用一下. 省下的就是等MS的ajax出来再研究就行了.
可恨live writer 不支持插入code. 每次贴code都很费劲
.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
5
<html xmlns="http://www.w3.org/1999/xhtml" >
6
<head runat="server">
7
<title>Untitled Page</title>
8
<script type="text/javascript">
9
10
function GetServerTime()
11
{
12
var message = '';
13
var context = '';
14
15
<%=sCallBackFunctionInvocation%>
16
}
17
18
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
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
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
8
public partial class Default2 : System.Web.UI.Page, ICallbackEventHandler
9
{
10
public string sCallBackFunctionInvocation;
11
protected void Page_Load(object sender, EventArgs e)
12
{
13
ClientScriptManager cs = Page.ClientScript;
14
sCallBackFunctionInvocation = cs.GetCallbackEventReference(this, "message", "ShowServerTime", "context", true);
15
}
16
17
public void RaiseCallbackEvent(String eventArgument)
18
{
19
sCallBackFunctionInvocation = eventArgument + DateTime.Now.ToString();
20
}
21
public string GetCallbackResult()
22
{
23
return sCallBackFunctionInvocation;
24
}
25
}
26
如果不想去别的网站 截取内容, 再加点javascript不就实现了传说中的ajax功能了么? 还去研究那么赋值ajax libraries干嘛呢, 不过rico的drag-drop功能,还是很眩的, 可以用一下. 省下的就是等MS的ajax出来再研究就行了.
可恨live writer 不支持插入code. 每次贴code都很费劲
相关文章推荐
- Custom Script Callbacks in ASP.NET 2.0 without refresh whole Page
- Script Callback in ASP.NET 2.0 (脚本回调)
- Script Callback in ASP.NET 2.0 (脚本回调,无刷新访问数据)
- ASP.NET 2.0 ClientScript Callback
- 设置页面的默认焦点控件和默认接受事件控件How Do I Set the DefaultFocus or DefaultButton in a Page Based on a Master Page in ASP.NET 2.0
- Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
- Explain the page life cycle in ASP.NET 2.0
- Cross page posting in ASP.NET 2.0
- cross-page posting in ASP.NET 2.0
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- Rewrite the master page form action attribute in asp.net 2.0
- Dynamically Setting the Page's Title in ASP.NET 2.0 or By SiteMap
- How to programmatically assign a SkinID to a control while using a master page in ASP.net 2.0
- ASP.NET 2.0中的脚本回调(Script Callback) [转]
- Rewrite the master page form action attribute in asp.net 2.0
- Page Life cycle methods in ASP.NET 2.0
- Script Callbacks in ASP.NET
- ASP.NET 2.0 的 Client Script Callback
- 我的AjaxPage对象(Asp.NET 1.1)-借用2.0的Callback实现无刷新回调
- Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0