1、准备:下载AjaxPro.2.dll
2、应用:项目中引用AjaxPro.2.dll
3、web.config中引用:
<system.web> <httpHandlers> <add verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/> </httpHandlers> </system.web>
public partial class test2 : System.Web.UI.Page { protected void Page_Load(object sender,EventArgs e) { if (!IsPostBack) { } AjaxPro.Utility.RegisterTypeForAjax(typeof(test2)); --test2为页面类名 } //根据传递过来的商品规格获取商品信息 [AjaxPro.AjaxMethod] --声明AjaxPro public string GetProductInfoByDetail(string guid,string detail) { string refs = "0"; if (detail != "") { string price = ""; string proNum = ""; string sql = "select * from tb1 where guid='" + guid + "' and detail='" + detail + "'"; DataTable dt = mallCtr.sqlTable(sql); if (dt.Rows.Count > 0) { price = dt.Rows[0]["price"].ToString(); proNum = dt.Rows[0]["RepertoryCount"].ToString(); refs = price + "$$$" + proNum; } else { refs = "1"; } } return refs; } }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="test2.aspx.cs" Inherits="test2" %> <!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>测试AjaxPro</title> <script language="javascript" defer=true> function GetProductNum() { refs=test2.GetProductInfoByDetail(document.getElementById("<%=TextBox1.ClientID %>").value,document.getElementById("<%=TextBox2.ClientID %>").value).value; //获取后台返回值 if (refs!="0") { //alert(refs); var val=refs.split('$$$'); document.getElementById("<%=TextBox3.ClientID %>").value=val[0]; //给textBox赋值 document.getElementById("Label2").innerHTML = val[1]; //给label赋值 } } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="GetProductNum();"/> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> <asp:Label ID="Label2" runat="server" Text="aa"></asp:Label> <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> </div> </form> </body> </html>
以上方式只能部署在Windows Server 2003+ IIS 6.0上,如部署在Windows Server 2008 + IIS 7.0上,除上述步骤外,还需要另外设置,具体如下:
1、在<system.webServer><handlers>下增加节点
<add name="ajaxpro" verb="*" path="*.ashx"type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
<!--程序部署在windows server 2008+IIS7.0,需要在此处声明AjaxPro2,才能正常使用--> <system.webServer> <handlers> <add name="ajaxpro" verb="*" path="*.ashx" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/> </handlers> </system.webServer>
或:
2、把应用程序池改为“经典模式”
下载地址: 利用AjaxPro2实现前后台数据交互实例下载