反射+特性打造简洁的AJAX调用
这里我要实现类似AjaxPro组件调用效果的功能,先看看AjaxPro在CS文件中的代码是怎么写的。
//在后台写的有参方法 [AjaxPro.AjaxMethod] public string getString(string str) { return str + "Say: hello my friends"; }
前台页面的调用方式
function Button4_onclick() { var str=document.getElementByIdx_x("<%=TextBox1.ClientID %>").value; WebUI._Default.getString(str,getStringCallBack); }
这样的调用代码相当于在客户端直接执行了服务器端代码,然后取到执行结果,是不是有点意思,接下来我们就来自己把这类似的功能实现一把。
不过在实现之前你最好对反射和特性的基本运用有一定的了解,马上进入正题。
首先我们来理一下实现的思路
1.定义能应用在方法上的Ajax标识特性,因为并不是Page对象的所有方法都需要公开被调用,可以用特性做一个标记,可以通过MethodAliasName属性为方法设置一个方法别名,这样做的目的主要是为了安全和AJAX调用代码更简洁。
using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace AjaxInvokeDemo.Ajax { [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)] public class AjaxMethodAttribute : Attribute { /// <summary> /// 设置ajax调用时的方法别名 /// </summary> public string MethodAliasName { get; set; } } }
2.定义页面基类,在里面实现一些公用的逻辑。比如获取要执行的方法的名称以及方法的参数,并通过反射对方法进行调用。具体还是看代码吧,可能更清楚一点。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime; using System.Reflection; using System.Collections.Specialized; namespace ConfigDemo.Ajax { public abstract class AjaxPage<T> : System.Web.UI.Page { private static readonly string AjaxMethodCacheName = "AjaxMethods"; private string pageName = typeof(T).Name; private Type pageType = typeof(T); protected override void OnPreInit(EventArgs e) { if (Cache[AjaxMethodCacheName] == null) { AjaxMethods = AjaxHelper.GetAjaxMethods<T>(); Cache[AjaxMethodCacheName] = AjaxMethods; } else { AjaxMethods = Cache[AjaxMethodCacheName] as Dictionary<string, string>; } base.OnPreInit(e); } protected override void OnLoad(EventArgs e) { ExecuteAction(); base.OnLoad(e); } public Dictionary<string, string> AjaxMethods { get; set; } /// <summary> /// Action名称或者别名 /// </summary> public string CurrentActionName { get { if (!string.IsNullOrEmpty(HttpContext.Current.Request["action"])) { return HttpContext.Current.Request["action"]; } throw new Exception("调用时必须指定Action参数.."); } } /// <summary> /// 传递的参数集合 /// </summary> public object[] CurrentParams { //一般的调用URL格式为 http://xxxxx/yyy.aspx?action=method&id=a001 ... get { NameValueCollection NVs = HttpContext.Current.Request.QueryString; object[] objs = new object[NVs.Count-1]; //这里将action参数排除在外 for(int i =1;i<NVs.Count; i++) { objs[i-1] = NVs[i]; } return objs; } } /// <summary> /// 最终要执行的Page的方法的真实名称 /// </summary> public string CurrentAjaxMethodName { get; set; } public void ExecuteAction() { if (!AjaxMethods.ContainsKey(CurrentActionName) && !AjaxMethods.ContainsValue(CurrentActionName)) { throw new Exception("调用的方法不存在或者未被公开为Ajax方法.."); } //没有为Ajax方法指定别名 if (AjaxMethods.ContainsKey(CurrentActionName) && AjaxMethods[CurrentActionName] == CurrentActionName) { CurrentAjaxMethodName = CurrentActionName; } else { //为Ajax方法指定了别名 CurrentAjaxMethodName = AjaxMethods.First<KeyValuePair<string, string>>(x => x.Value == CurrentActionName).Key; } MethodInfo method = pageType.GetMethod(CurrentAjaxMethodName); if (method != null) { object PageObj = GetPageInstance(); method.Invoke(PageObj, CurrentParams); } } public object GetPageInstance() { if (Cache[pageName] == null) { object PageObj = Activator.CreateInstance(pageType); return Cache[pageName] = PageObj; } else { return Cache[pageName] as object; } } } }
3.为了获取被标识为AjaxMethod的方法集合,这里专门用一个AjaxHelper类实现。
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Reflection; namespace ConfigDemo.Ajax { public class AjaxHelper { /// <summary> /// 获取指定Page对象中标记为Ajax的方法集合 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="page"></param> /// <returns></returns> public static Dictionary<string,string> GetAjaxMethods<T>() { Dictionary<string, string> ajaxMethods = new Dictionary<string, string>(); AjaxMethodAttribute ajaxMethodAttribute = null; //方法别名 string methodAliasName = string.Empty; Type t = typeof(T); MethodInfo[] methods = t.GetMethods(); foreach (MethodInfo method in methods) { object[] attributes = method.GetCustomAttributes(typeof(AjaxMethodAttribute), false); if (attributes != null && attributes.Length > 0) { ajaxMethodAttribute = (AjaxMethodAttribute)attributes[0]; //如果没有为ajax调用方法设置别名则用方法本身的名称 methodAliasName = string.IsNullOrEmpty(ajaxMethodAttribute.MethodAliasName) == true ? method.Name : ajaxMethodAttribute.MethodAliasName; ajaxMethods.Add(method.Name, methodAliasName); } } return ajaxMethods; } } }
4.基础框架搭建完毕,看看怎么使用
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Reflection; using System.Text; namespace AjaxInvokeDemo.Ajax { public partial class Index : AjaxPage<Index> { protected void Page_Load(object sender, EventArgs e) { } [AjaxMethod] public void GetPerson(string id) { List<Person> ps = new List<Person>(); ps.Add(new Person { Id = "a001", Name = "abc", Sex = "男", Age = 20 }); ps.Add(new Person { Id = "a002", Name = "xyz", Sex = "女", Age = 18 }); ps.Add(new Person { Id = "a003", Name = "zzzz", Sex = "男", Age = 28 }); ps.Add(new Person { Id = "a004", Name = "yyy", Sex = "男", Age = 21 }); StringBuilder builder = new StringBuilder(); Person person = ps.Find(p => p.Id == id); if (person != null) { //http://htmltextwriterutil.codeplex.com/ 这是htmltextwriter工具的下载地址,一款拼字符串的好工具 builder.Append(" <ul>"); builder.AppendFormat(" <li>编号:{0}</li>",person.Id); builder.AppendFormat(" <li>姓名:{0}</li>", person.Name); builder.AppendFormat(" <li>性别:{0}</li>", person.Sex); builder.AppendFormat(" <li>年龄:{0}</li>", person.Age); builder.Append(" </ul>"); } HttpContext.Current.Response.Write(builder.ToString()); HttpContext.Current.Response.End(); } } public class Person { public string Id { get; set; } public string Name { get; set; } public string Sex { get; set; } public int Age { get; set; } } }
这里的使用有两个要点,一是页面要继承自AjaxPage<T>泛型类,第二是在要被客户端调用的方法上加上[AjaxMethod]特性,一切就这么简单。。
5.客户端调用
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AjaxTest.aspx.cs" Inherits="AjaxInvokeDemo.Ajax.AjaxTest" %> <!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> <script type="text/javascript" src="../Scripts/jquery-1.4.1.min.js"></script> <script type="text/javascript"> $(function(){ $("#btn").click(function(){ $("#box").load("Index.aspx?action=GetPerson&id="+$("#txtId").val()); }); }); </script> </head> <body> <form id="form1" runat="server"> 用户ID:<input type="text" id="txtId" value="a001"/><input type="button" id="btn" value="查询"/> <div id="box"> </div> </form> </body> </html>
至此,调用完毕。。。
最后,还是附上代码吧。下载网站项目之后里面你会看到一个叫做AJAx的文件夹,里面就是所有代码了,运行AjaxTest.aspx页面就可以直接体验效果了。文章写的比较匆忙,如果你有任何疑问或建议都请告诉我。。。好啦,就到这里吧
文件下载:下载
相关推荐
asp.net ajax调用aspx、ajax调用ashx处理类方法、ajax调用webservice方法,返回多种数据类型等xml、html等。
Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 内容 C#Spy++调用 ...
ajax调用xml ajax调用htmlajax调用xml ajax调用htmlajax调用xml ajax调用htmlajax调用xml ajax调用htmlajax调用xml ajax调用htmlajax调用xml ajax调用htmlajax调用xml ajax调用htmlajax调用xml ajax调用htmlajax调用...
jsp+servlet+ajax使用json作为数据传输介质完成 1.用户名是否存在的验证 2.根据姓名获取该对象使用gson将对象转换成json后返回给客户端并显示,完成修改功能时经常使用 3.gson将集合转换成json(数组格式)后返回给...
主要用于消息提醒,利用js页面定时器,定时提交ajax请求,查询最新的消息记录。改了很多次,终于成功了。
JSP+jquery使用ajax方式调用json的实现方法.docx
Java中:struts2+jQuery+ajax调用(引用) 代码,解析,源码,demo,实例,分析
一个很美丽的瀑布流效果 自已用ASP程序写了调用数据库,写了Ajax加载 真是一级棒啊 分享出来
jquery与ajax调用,有代码 , 有说明,使ajax调用简单 需要使用 jquery调用ajax的朋友请下载!
本文实例讲述了JSP+jquery使用ajax方式调用json的实现方法。分享给大家供大家参考,具体如下: 前台: [removed][removed] [removed] //test function test(uid) { if(confirm(确定该用户操作+uid+吗?)...
接口编程 分层+反射+接口层GDI的使用图片添加水印效果
最简单的Ajax调用webservices
jQuery使用Ajax方法调用WebService.doc
1. Ajax调用无参的后台方法数据 2. Ajax调用有参后台方法中数据 3. Ajax调用后台方法返回数组数据 4. Ajax调用xml中的数据
JQ+ajax实现异步调用 一个简单的小例子
3.Ajax无刷新异步调用Struts2,返回Json数据,以用户注册为例。 4.在服务端分页查询功能,优点:实时性:跳页才查询。数据量小:只加载当前页的记录进行显示。 5.单数据源配置(兼容Tomcat和Weblogic)。 6.Hibernate...
PHP+AJAX实现调用数据瀑布流显示方式,本文件内包含:js库文件、css样式文件、php文件、html页面模板页面,欢迎各位学习下载
内有详细说明 Ajax调用.net后台函数 做到无刷新获取数据及可以随意调用后台方法和函数
使用js+ajax调用WCF服务,博客地址:http://blog.csdn.net/zaijianluoye110/article/details/25364945
此程序为使用ScriptManager+Ajax调用WCF服务(为服务定义接口),其中有些问题讨论,请参考博客:http://blog.csdn.net/zaijianluoye110/article/details/25361225