Custom Script Callbacks in ASP.NET 2.0 without refresh whole Page
来源:互联网 发布:tensorflow好难 编辑:程序博客网 时间:2024/05/21 17:21
这些天研究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
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
1using System;
2using System.Web;
3using System.Web.Security;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6using System.Web.UI.HtmlControls;
7
8public 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
1using System;
2using System.Web;
3using System.Web.Security;
4using System.Web.UI;
5using System.Web.UI.WebControls;
6using System.Web.UI.HtmlControls;
7
8public 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 Callbacks in ASP.NET
- cross-page posting in ASP.NET 2.0
- Cross page posting in ASP.NET 2.0
- Custom Paging in ASP.NET 2.0 with SQL Server 2005
- Explain the page life cycle in ASP.NET 2.0
- ASP.Net 2.0 Client Callbacks, makes remote scripting easier
- Strongly typed page references in ASP.NET
- Get the page name in asp.net web page
- ASP.NET 2.0 Page LifeCycle
- Writing a Custom Membership Provider for the Login Control in ASP.NET 2.0
- Implement Custom Paging in the ASP.Net Datagrid Control...
- Part 82 - Creating custom validation attribute in asp.net mvc
- Custom Configuration Section Handler in .NET 2.0
- Script Callback in ASP.NET 2.0 (脚本回调,无刷新访问数据)
- Loading an ASP.NET Page Class dynamically in an HttpHandler
- asp.net 2.0中的Cross PAGE REQUEST
- ASP.NET 2.0 Page 加载的过程
- CSDN技术招聘
- SQL数据导入导出
- 搜索 等待
- Peer Review 该如何翻译
- 开源的iis urlrewrite:IIRF
- Custom Script Callbacks in ASP.NET 2.0 without refresh whole Page
- 装电脑
- 关于mysql的limit
- windows路由表完全掌握(内容:路由表解析,读懂路由表)
- 探求学习嵌入式系统的最优解-连载1
- Sql语句中IN等方面的用法
- 什么是EJB
- sizeof 操作符
- 如何在xcode中进行依赖性编译