To learn more about AJAX, visit: http://en.wikipedia.org/wiki/AJAX http://www.adaptivepath.com/publications/essays/archives/000385.php |
'VB.Net
public function
Add(firstNumber
as
integer
, secondNumber
as
integer
)
as integer
return
firstNumber + secondNumber
end sub
|
//C#
public int
Add(
int
firstNumber,
int
secondNumber)
{
return
firstNumber + secondNumber;
}
|
If you’re having difficulty setting up the reference, check out:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskaddingremovingreferences.asp
|
type =" Ajax.PageHandlerFactory, Ajax " />
...
|
'vb.net
Public
Class
Index
Inherits
System.Web.UI.Page
Private Sub
Page_Load(sender
As
Object, e
As
EventArgs)
Handles MyBase
.Load
Ajax.Utility.RegisterTypeForAjax(
GetType
(Index))
'...
end sub
'...
End Class
|
//C#
public class
Index
:
System.Web.UI.Page{
private void
Page_Load(
object
sender, EventArgs e){
Ajax.Utility.RegisterTypeForAjax(
typeof(
Index
)
);
//...
}
//...
}
|
|
NAMESPACE.PAGECLASS
|
The namespace and class of the current page
(this will typically be the value of the
Inherits attribute in the
@Page directive)
|
ASSEMBLYNAME
|
The name of the assembly the current page is part of
(this will typically be the name of your project)
|
<%@ Page
Inherits
="
AjaxPlay.Sample" Codebehind="sample.aspx.cs" ... %>
|
'VB.Net
Public
Function
ServerSideAdd
(
byval
firstNumber
As
Integer
,
byval
secondNumber
As Integer ) As Integer
Return
firstNumber + secondNumber
End
Function
|
//C#
[Ajax.AjaxMethod()]
public
int
ServerSideAdd
(
int
firstNumber,
int
secondNumber)
{
return
firstNumber + secondNumber;
}
|
<%@ Page
Inherits
="
AjaxPlay.Sample
"
Codebehind
="
sample.aspx.cs
"
...
%>
|
Sample.ServerSideAdd(100,99, ServerSideAdd_CallBack);
function
ServerSideAdd_CallBack(response){
if
(response.error != null){
alert
(response.error);
return
;
}
alert
(response.value);
}
|
value
|
The actual return value (be it a string, custom object or dataset) of the server-side function.
|
error
|
An error message, if any.
|
request
|
The raw response from the xml http request.
|
context
|
A context object.
|
To learn more about the Xml Http request, check out the following link dump:
http://www.quirksmode.org/blog/archives/2005/02/xmlhttp_linkdum.html
|
|
[Serializable()]
public
class
User{
private
int
_userId;
private
string
_firstName;
private
string
_lastName;
public
int
userId{
get
{
return
_userId; }
}
public
string
FirstName{
get
{
return
_firstName; }
}
public
string
LastName{
get
{
return
_lastName; }
}
public
User(
int
_userId,
string
_firstName,
string
_lastName){
this
._userId = _userId;
this
._firstName = _firstName;
this
._lastName = _lastName;
}
public
User(){}
[AjaxMethod()]
public
static
User GetUser(
int
userId){
//Replace this with a DB hit or something :)
return
new
User(userId,"
Michael
", "
Schwarz
");
}
}
|
private
void
Page_Load(
object
sender, EventArgs e){
Utility.RegisterTypeForAjax(
typeof
(User));
}
|
|
Public
Class
AjaxFunctions
Public
Function
Validate(username
As
String,
password As
String)
As
Boolean
'do something
'Return something
End
Function
End
Class
|
'Vb.Net
Private
Sub
Page_Load(sender
As
Object, e
As
EventArgs)
Handles
MyBase
.Load
Ajax.Utility.RegisterTypeForAjax(
GetType
(AjaxFunctions))
'...
End Sub
|
//C#
private
void
Page_Load(
object
sender, EventArgs e){
Ajax.Utility.RegisterTypeForAjax(
typeof
(AjaxFunctions));
//...
}
|
[Ajax.AjaxMethod]
public
string
Test1(
string
name,
string
email,
string
comment){
string
html = "";
html += "
Hello
" + name + "
";
html += "
Thank you for your comment
";
html += System.Web.HttpUtility.HtmlEncode(comment);
html += "
.
";
return
html;
}
|
'Vb.Net
Public
Function
DocumentReleased()
As
ArrayList
If
HttpContext.Current.Session("DocumentsWaiting")
Is
Nothing
Then
Return
Nothing
End
If
Dim
readyDocuments
As
New ArrayList
Dim
documents()
As
Integer = CType(HttpContext.Current.Session("DocumentsWaiting"), Integer())
For
i
As
Integer
= 0 To documents.Length - 1
Dim
document
As
Document = document.GetDocumentById(documents(i))
If
Not
document
Is
Nothing
AndAlso document.Status = DocumentStatus.Ready
Then
readyDocuments.Add(document)
End
If
Next
Return
readyDocuments
End
Function
|
//C#
[Ajax.AjaxMethod(HttpSessionStateRequirement.Read)]
public
ArrayList DocumentReleased(){
if
(HttpContext.Current.Session["
DocumentsWaiting
"]
==
null
){
return
null
;
}
ArrayList readyDocuments =
new
ArrayList();
int
[] documents = (
int
[])HttpContext.Current.Session["
DocumentsWaiting
"];
for
(
int
i = 0; i < documents.Length; ++i){
Document document = Document.GetDocumentById(documents[i]);
if
(document !=
null
&& document.Status == DocumentStatus.Ready){
readyDocuments.Add(document);
}
}
return
readyDocuments;
}
}
|
onload
="setTimeout('
Document.DocumentReleased(DocumentsReady_CallBack)', 10000);">
|
Keep a close eye on the AJAX .Net wrapper website:
http://ajax.schwarz-interactive.de/
|
For a good hands-on sample, check out the following demo application:
http://ajax.schwarz-interactive.de/download/ajaxsample.zip
|
以下摘自翻译:
就听说Ajax技术了,传说中是一种很强的东西,号称无刷新,其实是在web上通过javascript,使用异步的xmlhttp请求,实现无刷新的web界面。可惜一直没有体验过,先后听做PHP的朋友用过PHP的Ajax开发包,而且做了很多很酷的东西,使小生羡慕不已。
今天下了一个.net Ajax开发包,该开发包包括ASP2.0和目前ASP1.1版使用的Ajax,详细地址参见http://ajax.schwarz-interactive.de/,接下来,开始。(下载请您点击:Ajax.dll)
1. 新建一个项目,在引用中添加引用Ajax.dll,Ajax.dll位于下载的压缩包里面。
2.建立HttpHandler,在web.config里面加上
3.新建一个类DemoMethods,这个类实现获取客户端MAC地址:
using System;
using System.Web;
namespace AjaxSample
{
/**
/// Summary description for Methods.
///
public class DemoMethods
{
[Ajax.AjaxMethod]
public string GetCustomerMac(string clientIP) //这里输入客户端IP,这个函数知识测试用,你也可以写一个其他的简单一点的函数代替
{
string mac = "";
System.Diagnostics.Process process = new System.Diagnostics.Process();
process.StartInfo.FileName = "nbtstat";
process.StartInfo.Arguments = "-a "+clientIP;
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
string output = process.StandardOutput.ReadToEnd();
int length = output.IndexOf("MAC Address = ");
if(length>0)
{
mac = output.Substring(length+14, 17);
}
process.WaitForExit();
return mac.Replace("-", "").Trim();
}
}
}
4.写javascript,新建一个名为default.js文件如下
function GetMac()
{
var clientIP="192.168.0.1";
//document.getElementById("Mac").value=DemoMethods.GetCustomerMac(clientIP).value
alert(DemoMethods.GetCustomerMac(clientIP).value);
}
5.在某个Aspx页面放上一个html 的button
在页面上
中引用default.js : 在INPUT的onclick事件中加上
οnclick="javascript:GetMac()"
value="客户端获取IP" οnclick="javascript:GetMac();">
6.在page页面的Page_Load事件中加上
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
Ajax.Utility.RegisterTypeForAjax(typeof(AjaxSample.DemoMethods));
}
注意:typeof(AjaxSample.DemoMethods)中,AjaxSample是命名空间,DemoMethods是要包含要调用方法的类,即上面第3步.新建类DemoMethods
7.修改Global.asax的Application_Start事件,设置Ajax的HandlerPath :
protected void Application_Start(Object sender, EventArgs e)
{
Ajax.Utility.HandlerPath = "ajax";
}
运行看看效果。是不是没有刷新就在服务器端取到客户端的MAC地址?
需要注意的是:该版本的.net Ajax需要手工在中Global.asax加上Ajax.Utility.HandlerPath = "ajax"; 配置文件web.config必须加上HttpHandler的配置信息!
该开发包的新版本还没有来得及体验,估计新版本中会方便一些,可能会去掉手动的设置Global.asax的Application_Start事件中加上Ajax.Utility.HandlerPath = "ajax";以及其他麻烦的设置!期待……