您的位置:首页 > 编程语言 > ASP

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都很费劲
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: