Asp.Net中Ajax应用

好久没写文章了,呵呵,最近在研究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) "   />

 

2.继承IHttpHandler接口

using  System;
using  System.Web;
using  System.Web.Script.Serialization;
实现IHttpHandler 接口

在以下页面中调用
< 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,这部分内容待续。。。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值