好久没写文章了,呵呵,最近在研究Asp.Net中的Ajax应用,也该略微来点总结了。Asp.Net中应用Ajax感觉真的很简单,一般有三种应用,1.纯粹在在客户端定义Javascript;2.继承IHttpHandler接口;3.使用WebService
在Asp.NET中使用Ajax的前提:首先必须安装微软Ajax安装包,必须有Javascript和html基础。
下面分别说明下使用上述3中方式:
1.在客户端定义javascript脚本
<
asp:ScriptManager ID
=
"
ScriptManager1
"
runat
=
"
server
"
/>
//
必须的,这样才能让.NET知道,你要使用Ajax
< script language = " javascript " type = " text/C# " >
Person.registerClass( " Person " );
function Person(firstName,lastName)
... {
this.firstName = firstName;
this.lastName = lastName;
this.show = function()...{ return this.firstName + this.lastName; }
}
Employee.registrClass( " Employee " ,Person);
function Employee(firstName,lastName,title)
... {
Employee.initializeBase(this,(firstName,lastName));
this.title = title;
this.showE = function() ...{return this.firstName + this.lastName + this.title; }
}
</ script >
< input type = " button " onclick = " alert(new Person('Ken','Chen').show) " />
< input type = " button " onclick = " alert(new Person('Jay','Zhou','CEO').showE) " />
< script language = " javascript " type = " text/C# " >
Person.registerClass( " Person " );
function Person(firstName,lastName)
... {
this.firstName = firstName;
this.lastName = lastName;
this.show = function()...{ return this.firstName + this.lastName; }
}
Employee.registrClass( " Employee " ,Person);
function Employee(firstName,lastName,title)
... {
Employee.initializeBase(this,(firstName,lastName));
this.title = title;
this.showE = function() ...{return this.firstName + this.lastName + this.title; }
}
</ script >
< input type = " button " onclick = " alert(new Person('Ken','Chen').show) " />
< input type = " button " onclick = " alert(new Person('Jay','Zhou','CEO').showE) " />
2.继承IHttpHandler接口
using
System;
using System.Web;
using System.Web.Script.Serialization;
实现IHttpHandler 接口 #region 实现IHttpHandler 接口
public class GetEmployee : IHttpHandler ...{
public void ProcessRequest (HttpContext context) ...{
context.Response.ContentType = "text/plain";
string firstName = context.Request.Params["firstName"];
string lastName = context.Request.Params["lastName"];
string title = context.Request.Params["title"];
Employee employee = new Employee(firstName, lastName, title);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonEmp = serializer.Serialize(employee);
context.Response.Write(jsonEmp);
}
public bool IsReusable ...{
get ...{
return false;
}
}
}
#endregion
在以下页面中调用
< asp:ScriptManager ID = " ScriptManager1 " runat = " server " >
</ asp:ScriptManager >
< script language = " javascript " type = " text/javascript " >
function ShowEmployee(firstName,lastName,title)
... {
var request = new Sys.Net.WebRequest();
request.set_url("GetEmployee.ashx");
request.set_httpVerb("POST");
request.add_completed(onGetEmployeeSuccess);
var requestBody = String.format(
"firstName={0}&lastName={1}&title={2}",
encodeURIComponent(firstName),
encodeURIComponent(lastName),
encodeURIComponent(title));
request.set_body(requestBody);
request.invoke();
}
function onGetEmployeeSuccess(response)
... {
if(response.get_responseAvailable)
...{
var employee = response.get_object();
alert(String.format(
"Hello I'm {0} {0},my tilte is {2}",
employee.FirstName,employee.LastName,
employee.Title))
}
}
</ script >
< input type = " button " value = " who " onclick = " ShowEmployee('Ken','Chen','CEO') " />
using System.Web;
using System.Web.Script.Serialization;
实现IHttpHandler 接口 #region 实现IHttpHandler 接口
public class GetEmployee : IHttpHandler ...{
public void ProcessRequest (HttpContext context) ...{
context.Response.ContentType = "text/plain";
string firstName = context.Request.Params["firstName"];
string lastName = context.Request.Params["lastName"];
string title = context.Request.Params["title"];
Employee employee = new Employee(firstName, lastName, title);
JavaScriptSerializer serializer = new JavaScriptSerializer();
string jsonEmp = serializer.Serialize(employee);
context.Response.Write(jsonEmp);
}
public bool IsReusable ...{
get ...{
return false;
}
}
}
#endregion
在以下页面中调用
< asp:ScriptManager ID = " ScriptManager1 " runat = " server " >
</ asp:ScriptManager >
< script language = " javascript " type = " text/javascript " >
function ShowEmployee(firstName,lastName,title)
... {
var request = new Sys.Net.WebRequest();
request.set_url("GetEmployee.ashx");
request.set_httpVerb("POST");
request.add_completed(onGetEmployeeSuccess);
var requestBody = String.format(
"firstName={0}&lastName={1}&title={2}",
encodeURIComponent(firstName),
encodeURIComponent(lastName),
encodeURIComponent(title));
request.set_body(requestBody);
request.invoke();
}
function onGetEmployeeSuccess(response)
... {
if(response.get_responseAvailable)
...{
var employee = response.get_object();
alert(String.format(
"Hello I'm {0} {0},my tilte is {2}",
employee.FirstName,employee.LastName,
employee.Title))
}
}
</ script >
< input type = " button " value = " who " onclick = " ShowEmployee('Ken','Chen','CEO') " />
3.使用WebService,这部分内容待续。。。