zoukankan
html css js c++ java
用最简单的方法实现Ajax
Aspx文件:
<
script
type
="text/javascript"
>
function
callToServer()
{
var
param
=
document.getElementById(
"
txtMessage
"
).value
+
"
&pa
"
+
document.getElementById(
"
a
"
).value;
var
context
=
""
;
document.getElementById(
"
a
"
).value
=
document.getElementById(
"
a
"
).value;
//
WebForm_InitCallback();
<%
=
callbackScript
%>
}
function
handleResultFromServer(result)
{
document.getElementById(
"
txtResult
"
).innerHTML
=
result;
}
</
script
>
<
form
id
="form1"
runat
="server"
>
<
div
><
INPUT
id
="txtMessage"
/>
a
<
input
id
="a"
name
="a"
/>
<
INPUT
onclick
="callToServer();"
type
="button"
value
="Call to Server"
/>
<
br
/>
<
span
style
="color:#ff0000"
></
span
>
Result :
<
div
id
="txtResult"
></
div
>
</
div
>
</
form
>
Aspx.cs文件:
public
partial
class
Test : System.Web.UI.Page, System.Web.UI.ICallbackEventHandler
{
protected
string
callbackScript
{
get
{
ClientScriptManager cm
=
Page.ClientScript;
//
return this.GetCallbackEventReference(this, "param", "handleResultFromServer", "context");
return
cm.GetCallbackEventReference(
this
,
"
param
"
,
"
handleResultFromServer
"
,
"
context
"
);
}
}
private
string
arg
=
""
;
protected
void
Page_Load(
object
sender, EventArgs e)
{
}
public
void
RaiseCallbackEvent(
string
eventArgument)
{
arg
=
eventArgument
+
Request[
"
a
"
];
for
(
int
i
=
0
; i
<
Request.Params.Count; i
++
)
{
arg
+=
"
Name:
"
+
Request.Params.GetKey(i)
+
"
:Value:<span style=\
"
color:#ff0000\
"
>
"
+
Request.Params[i]
+
"
</span><br/>
"
;
}
}
public
string
GetCallbackResult()
{
return
"
客户端在[
"
+
DateTime.Now.ToString()
+
"
]传送来 [
"
+
arg
+
"
].
"
;
}
}
这里主要是用到了Asp.net自带的 System.Web.UI.ICallbackEventHandler接口。
实现的这个接口的控件(包括页面)都可以以异步的方式用服务器发送数据。还可以设置一个回调函数。这个方法有两个局限:函数的返回类型只能是字符串类型。方法只有一个参数(可以通过分割字符串实现多个参数,但不安全。)
查看全文
相关阅读:
【Yii2.0】1.5 Yii2.0新特性记录
【PHP7.0】PHP7.0 小知识点摘录
【PHP7.0】PHP7.0学习笔记目录
【Yii2.0】1.4 Apache2.4.23基于主机名的虚拟主机配置
【Yii2.0】2.2 Yii2.0 Basic代码中路由链接被转义的处理
【Yii2.0】1.3 MySQL5.7.15修改root密码
[Leetcode 106] 130 Surrounded Regions
[Leetcode 105] 90 Subsets II
[Leetcode 104] 131 Palindrome Partitioning
[Leetcode 103] 37 Sudoku Solver
原文地址:https://www.cnblogs.com/heys/p/822770.html
最新文章
最近给另外一个部门帮忙的感悟和总结
Auto refresh mybatis
springboot项目起步
springboot配置全系列
OJ使用心得
MSF基础 在BT5 R3下的更新
ettercap 使用及在渗透论文中的作用
Generate Parentheses
N-Queens
Sort Colors
热门文章
N-Queens II
Climbing Stairs
Best Time to Buy and Sell Stock II
Binary Tree Level Order Traversal II
Binary Tree Level Order Traversal
Binary Tree Inorder Traversal
Binary Tree Postorder Traversal
【MediaWiki】MySQL中varbinary转varchar
【Electron】Electron的一些问题
【Electron】Electron在Windows下的环境搭建
Copyright © 2011-2022 走看看