扩展:如果多处调用而且调用方法较长,你当然可以再对应每个业务类做个js文件,将其中的调用放在一起。
==============================================================================
前段时间要用到Ajax,不过,完全靠自己来写嫌太烦,不愿做些麻烦事。
碰巧看到了ajaxpro,尝试了一下,觉得很简单,很实用,兼容性也很好。
AjaxPro的官网是http://ajaxpro.info,你可以下载到最新的AjaxPro组件。如果你想下载,可以点里这里。
在下载的的文件中,有VS2005的模板,安装了VS2005模板,就可以在VS2005中直接建立AjaxProWebSite了,在建立的AjaxProWebSite中,有一个默认的Demo,其实通过它,就能够完全了解AjaxPro的用法。
如果你用的是.NET2.0,且不用VS2005的话。你只需要把AjaxPro.2.dll放入应用程序的bin文件夹中,而且也只需要如下几步:
1、修改web.config
在system.web节点下添加
<system.web>
<httpHandlers>
<add verb="POST,GET" path="ajaxpro/*.ashx" type="AjaxPro.AjaxHandlerFactory, AjaxPro.2"/>
</httpHandlers>
2、将你的.NET方法添加AjaxMethod属性
[AjaxPro.AjaxMethod]
public DateTime GetServerTime()
{
return DateTime.Now;
}
3、在.NET方法中向客户端注册javascript,用以javascript使用
namespace MyDemo
{
public class _Default
{
protected void Page_Load(object sender, EventArgs e)
{
AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));
}
[AjaxPro.AjaxMethod]
public DateTime GetServerTime()
{
return DateTime.Now;
}
}
}
4、在客户端用javascript调用服务器端的方法,语法也很简单
function getServerTime()
{
MyDemo._Default.GetServerTime(getServerTime_callback); // asynchronous call
}
// This method will be called after the method has been executed
// and the result has been sent to the client.
function getServerTime_callback(res)
{
alert(res.value);
}
就这样,简单的几步,就已经完成了。在客户端用javascript异步调用服务器端的C#方法,并可以得到服务器端的返回值,这个值会传到javascript,javascript可以处理这个返回的值,这个示例是官方给出的示例,服务器端返回的是一个DateTime,不过,我们也可以返回复杂的数据类型,像DataTable之类,总之,AjaxPro把Ajax搞得很简单。
不过,还有一点我没完全弄清楚,就是AjaxPro.OnLoading,它可以在服务器未发送回值的时候向客户显示的loading一类的东西;我在使用过程中,有时会出现AjaxPro没有定义的情况。关于这一点,也在google groups上看到相关解答,不过自己还是没有完全搞清楚
===============================================================================
AjaxPro可以写Session在服务器端page_load
AjaxPro.Utility.RegisterTypeForAjax(typeof(test));
this.Button_Write.Attributes.Add("onclick","WriteSession();");//写session
this.Button_Read.Attributes.Add("onclick", "ReadSession();");//读session
其他写和读的方法
/// <summary>
/// 写session
/// </summary>
/// <param name="str"></param>
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public void WriteSession(string str)
{
Session["UserName"] = str;
}
/// <summary>
/// 读session
/// </summary>
/// <returns></returns>
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public string ReadSession()
{
string str = "";
if (Session["UserName"] != null)
{
str = Session["UserName"].ToString();
}
return str;
}
客户端代码:
//4、访问Session的值
//写入
function WriteSession()
{
var str = "HAHA";
test.WriteSession(str,CallBack_WriteSession);
}
function CallBack_WriteSession(res)
{
if(res.error == null)
{
alert("OK");
}
else
{
alert(res.error.message);
}
}
//访问
function ReadSession()
{
test.ReadSession(CallBack_ReadSession);
}
function CallBack_ReadSession(res)
{
if(res.error == null)
{
alert(res.value);
}
else
{
alert(res.error.message);
}
}
=============================================================================
基本功能实现,下一步当然是更进一步的实践了。接着做的例子是个简单的登录操作,并将用户名和密码用Session记录下来。代码和上面的类似,就不贴了,占地方,呵呵。
但有2点需要注意
1 关于Session,如果想在AjaxMethod中使用Session的话,那么AjaxMethod标签必须带AjaxPro.HttpSessionStateRequirement.ReadWrite参数
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public string CheckPassword()
{
}
2 关于属性,我们注册了一个属性,运行之后,在客户端JS中的确就可以访问了
[AjaxPro.AjaxProperty()]
public string UserName
{
set
{
Session["UserName"] = value;
}
get
{
return Session["UserName"].ToString();
}
}
AjaxProSample.SampleSession.UserName = document.getElementById("txtUserName").value;
alert(AjaxProSample.SampleSession.UserName);
但此时如果在服务器端代码中企图使用的话,就会出现空引用异常,如果非要在客户段和服务器端同时使用这个属性的话,请增加一个设置值的方法。例如:
[AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
public void SetValue(string value)
{
UserName = value;
}
AjaxProSample.SampleSession.UserName = document.getElementById("TextBox1").value;
AjaxProSample.SampleSession.SetValue(AjaxProSample.SampleSession.UserName);
如对本文有疑问,请提交到交流论坛,广大热心网友会为你解答!! 点击进入论坛