用AjaxPro2遇到的问题

来源:互联网 发布:外贸海关数据 编辑:程序博客网 时间:2024/05/16 14:41

 

我在写代码时,太马虎 。在客户端少写了.value。真晕
我做了一个超简单的 AjaxProDemo ,发上来供初学者用!!!
步骤如下:
1. 添加 AjaxPro.dll 文件的引用(示例代码中已经包含,直接COPY过来使用即可).

 2. 在Web.config文件中添加以下配置,           

<httpHandlers>             <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro" />            
</httpHandlers>


3. 在要使用AjaxPro.NET框架的页面 *.aspx.cs 的 Page_Load事件中加如下代码:

 
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
 
 4. 经过以上三步骤后, 只要在后台服务端的方法前面增加属性[AjaxMethod]后:
 
  [AjaxMethod()]    // or [AjaxPro.AjaxMethod] 
 public string GetString( string val)
  {
      return val;
 
 
就可以在客户端直接使用服务端方法, 非常方便, 客户端调用后台代码如下:
var returnValue = 后台代码类名.GetString(参数);

具体如下:

web.config文件

<?xml version="1.0"?>
<configuration>
 <appSettings/>
 <connectionStrings/>
 <system.web>
  <httpHandlers>
   <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro" />
  </httpHandlers>
  
  <compilation debug="true"/>
  
  <authentication mode="Windows"/>
  
 </system.web>
</configuration>
 

Default.aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div>
    
<script type="text/javascript">
        function A(v)
{
            var res 
= _Default.GetString(v);
            var val 
= res.value;
            alert(val);
        }

    
</script>
    
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    
</div>
    
</form>
</body>
</html>

Default.aspx.cs文件:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using AjaxPro;
public partial class _Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    {
        Utility.RegisterTypeForAjax(
typeof(_Default) );
        TextBox1.Attributes.Add(
"onblur""A(this.value);");
    }
    [AjaxPro.AjaxMethod]
    
public string GetString(string val)
    {
        
return val;
    }
}

记得要引用AjaxPro.dll文件!!

 

原创粉丝点击