积极答复者
【AJAX】ajax可以调用某cs页面中的方法么?

问题
-
原先使用AJAX的时候服务器处理程序都是要新建一个页面来处理,每一个AJAX服务器程序都要建一个单独的页面交互,发现这样做非常费事。请问高手有没有办法可以指定某cs页面中的某一个方法呢?
比如:a.cs页面中有一个aa方法,如下:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
public partial class a : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//该页面加载时处理程序.大多数情况下,我在处理AJAX请求的时候都是在这里进行的
}
private void aa()
{
//执行代码
}
}
前台ajax程序如下:
function Method()
{
var objxml=createXMLHTTP();//声明XMLHttp
objxml.open("get","a.aspx",false);
objxml.send(null);
if(objxml.readyState=="4" && objxml.status=="200")
{
alert('插入成功!');
}
}
现在问题来了,如何指定a.aspx后台的aa方法呢?针对以上例子,也许有人会说直接在Page_Load中调用aa方法不就行了么?但根据我的程序第一次加载页面的时候不要执行该方法,只有点击按钮的时候执行AJAX程序。也许还有人要问加个ispostback不就行了么,是啊,也许能解决,(以上是我的看法不知道对不对)但我想说的是,能否绕过页面Page_Load方法,而直接指定AJAX所调用的后台方法呢?期待高手给予解答,谢谢!- 已移动 孟宪会Moderator 2010年3月21日 3:27 (发件人:.NET Framework 一般性问题讨论区)
答案
-
你好,
微软提供的ajax能很好的实现你的这种客户端调用服务器端方法的需求,干嘛还要用xmlhttp对象呢。
下面是个完整的例子。
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> [System.Web.Services.WebMethod] public static string ReturnTestString(string name) { return "Hello "+name+", this is a test for page method!"; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function pageLoad() { } function CallPageMethod() { PageMethods.ReturnTestString(document.getElementById("name").value,succedFunc,failedFunc); } function succedFunc(result) { alert(result); } function failedFunc(error) { alert(error); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" /> <input type="text" id="name" /> <input type="button" value="Page Method Demo" id="btnPageMethod" onclick="CallPageMethod();" /> </div> </form> </body> </html>
Microsoft Online Community Support- 已标记为答案 KeFang Chen 2010年3月24日 2:13
全部回复
-
你要引用AJAX.DLL组件,然后再JS中调用CS方法,
参照
http://hi.baidu.com/pangula/blog/item/c19bd01d4925038c87d6b619.html
努力+方法=成功 -
你好,
微软提供的ajax能很好的实现你的这种客户端调用服务器端方法的需求,干嘛还要用xmlhttp对象呢。
下面是个完整的例子。
<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> [System.Web.Services.WebMethod] public static string ReturnTestString(string name) { return "Hello "+name+", this is a test for page method!"; } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> function pageLoad() { } function CallPageMethod() { PageMethods.ReturnTestString(document.getElementById("name").value,succedFunc,failedFunc); } function succedFunc(result) { alert(result); } function failedFunc(error) { alert(error); } </script> </head> <body> <form id="form1" runat="server"> <div> <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" /> <input type="text" id="name" /> <input type="button" value="Page Method Demo" id="btnPageMethod" onclick="CallPageMethod();" /> </div> </form> </body> </html>
Microsoft Online Community Support- 已标记为答案 KeFang Chen 2010年3月24日 2:13