ie421.NET

面对技术,你别无选择,.NET世界是如此精彩,而我们要做的就是:Thinking More

博客园 首页 新随笔 联系 订阅 管理

1、配置文件:

<system.web>
   <httpHandlers>
    <add verb="GET" path="*.ashx" type="AjaxPro.AjaxBitmapHttpHandler,AjaxPro.2"/>
   </httpHandlers>

 

2、引用AjaxPro.2.dll

3、服务器端

   3。1:注册:AjaxPro.Utility.RegisterTypeForAjax(typeof(Test));//注册

   3。2:方法编写:   

   [AjaxPro.AjaxMethod]  //表明它们是可以被js代码所调用的
    public string CheckUserName(string name)
    {

          return "可以注册";
     }

 

4。客户端调用:

     <asp:TextBox ID="txtUserName" runat="server" CssClass="input" Columns="30" MaxLength="14" onblur="javascript:checkName();void(0);"></asp:TextBox><div id="errorMsg"></div>

<script language="javascript" type="text/javascript" defer="defer"> 


    
function checkName()
    {
        
var name=document.getElementById("<%=txtUserName.ClientID %>");
        document.getElementById(
"errorMsg").style.display="block";
        document.getElementById(
"errorMsg").style.color="red";
        document.getElementById(
"errorMsg").innerText=Test.CheckUserName(""+name.value+"").value;
        
    }
    
    
function checkAge()
    {
        
var age=document.getElementById("<%=txtAge.ClientID %>");
        document.getElementById(
"errorMsg1").style.display="block";
        document.getElementById(
"errorMsg1").style.color="red";
        document.getElementById(
"errorMsg1").innerText=Test.CheckAge(parseInt(age)).value;
        
    }
</script>

 

其中[AjaxPro.AjaxMethod]表明它们是可以被js代码所调用的,另外两个方法需要的参数类型不同,一个是string类型,一个是int类型,而js中数据没有类型的,因为都是var来声明的,所以如何把参数值和参数类型传给服务器还是一个麻烦问题。

查阅了资料,发现可以通过上述的方式解决:
Test.CheckUserName(""+name.value+""),服务器就自动把参数值当字符串类型来识别了;//""+参数值
Test.CheckAge(parseInt(age)),服务器就会把参数当整数来识别了。//parseInt(参数值)

 

 

<script type="text/javascript" defer="defer">

function doTest1() {
    AJAXDemo.Examples.Classes.Demo.GetMyClass(doTest1_callback);
}


function doTest1_callback(res) {
    
var p = res.value;
    alert(
"FirstName = " + p.FirstName + "\r\nFamilyName = " + p.FamilyName + "\r\nAge = " + p.Age);
    p 
= null;
}


function doTest2() {
    AJAXDemo.Examples.Classes.Demo.GetMyInheritedClass(doTest2_callback);
}


function doTest2_callback(res) {
    
var p = res.value;
    alert(
"FirstName = " + p.FirstName + "\r\nFamilyName = " + p.FamilyName + "\r\nAge = " + p.Age + "\r\n\r\nSizeInMeters = " + p.SizeInMeters + "\r\nID = " + p.ID);
    p 
= null;
}


function doTest3() {
    
var p = AJAXDemo.Examples.Classes.Demo.GetMyClass().value;        // synchronous call to the server-side method

    p.FirstName 
= "CLIENT-SIDE CHANGE";
    AJAXDemo.Examples.Classes.Demo.PutMyClass(p, doTest3_callback);
    p 
= null;
}


function doTest3_callback(res) {
    
var p = res.value;
    alert(
"FirstName = " + p.FirstName + "\r\nFamilyName = " + p.FamilyName + "\r\nAge = " + p.Age);
    p 
= null;
}


function doTest4() {
    
var p = AJAXDemo.Examples.Classes.Demo.GetPerson().value;        // synchronous call to the server-side method
    alert("p.FirstName = " + p.FirstName + "\r\n\r\nClick on OK to save this object using p.save();");
    
var b = p.save();
    alert(
"Person " + (b == true? "has been saved." : "could not be saved!"));

    alert(
"If we now set the .FirstName to \"HANS\" it will not work because\r\nthe Save method will not allow to save Person objects with that FirstName.");
    p.FirstName 
= "HANS";
    b 
= p.save();
    alert(
"Person with .FirstName == \"HANS\" " + (b == true? "has been saved." : "could not be saved!"));

    p 
= null;
    b 
= null;
}


</script>


 

posted on 2008-07-27 11:27  ie421  阅读(275)  评论(0编辑  收藏  举报