Ajax【ASP.NET 类库】当您懒得用 Json+Ajax 时,可以尝试 AjaxPro

貌似我们在页面调用Ajax时,要求通过 js 调用ajax
,接着后台处理获得的多少,然后再次回到数据,ajax 得到重临的多寡举办处理。

然而有没有能够像调用方法一致来开展Ajax操作呢? AjaxPro
能够办到。不难示例如下,先看功效图:

Ajax 1

主导正是 AjaxPro.2.dll 文件,简单计划一下就足以了,步骤如下:

1.引用 AjaxPro.2.dll 文件,配置 Web.config 文件:

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
    <httpHandlers>
      <add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
    </httpHandlers>
  </system.web>

  <system.webServer>
    <validation validateIntegratedModeConfiguration="false"/>
    <handlers>
      <add verb="*" path="*.ashx" name="AjaxPro" type="AjaxPro.AjaxHandlerFactory,AjaxPro.2"/>
    </handlers>
  </system.webServer>

</configuration>

2.然后就能够写想要处理的章程了:

namespace AjaxProTwo
{
    public class YlukaAjax
    {
        //AjaxPro.AjaxMethod        
        [AjaxMethod]
        public People GetThePeopleInfo(string name)
        {
            People people = new People();
            people.Name = name;
            people.BlogPath = @"http://www.cnblogs.com/yc-755909659/";
            return people;
        }

        //改变了方法名,调用时使用 AjaxProTwo.YlukaAjax.ShowMessage()
        [AjaxMethod, AjaxNamespace("ShowMessage")]
        public string ShowTheMessage()
        {
            return "你好,我是叶小超。很高兴认识你!";
        }
    }
}

3.终极,Web页面后台文件引用方法的类:

    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //必须引用,不能放在 IsPostBack 中 
            AjaxPro.Utility.RegisterTypeForAjax(typeof(YlukaAjax));
        }
    }

4.末段,js 调用就行了:

    <form id="form1" runat="server">
        <div>
            <input id="showInfo" type="button" onclick="show()" value="Show The Info" />
        </div>
        <div>
            <label>姓名:</label><label id="lb_Name" />
        </div>
        <div>
            <label>博客:</label><label id="lb_BlogPath" />
        </div>
        <div>
            <label>简介:</label><label id="lb_Message" />
        </div>
        <script type="text/javascript">
            function show() {
                var people = AjaxProTwo.YlukaAjax.GetThePeopleInfo("J灬叶小超").value;
                document.getElementById("lb_Name").innerHTML = people.Name;
                document.getElementById("lb_BlogPath").innerHTML = people.BlogPath;
                document.getElementById("lb_Message").innerHTML = AjaxProTwo.YlukaAjax.ShowMessage().value;
            }
        </script>
    </form>

 

源代码地址:AjaxProTwo.zip

参照文章:http://www.cnblogs.com/yaozhenfa/p/np\_ajaxpro.html

相关文章