|
 |
栏目导栏 |
|
| |
|
|
|
|
 |
资料搜索 |
|
| |
|
|
|
|
 |
热门文章 |
|
| |
|
|
|
|
 |
最新文章 |
|
| |
|
|
|
| |
| |
|
|
|
| |
| Asp.net Ajax:我可以用javascript做些什么 |
|
一直在关注AjaxControlToolkit的变化,期待着随着时间的推移这个工具包会日臻完善,不过就实际项目中应用的效果来讲,还是有点失望的。问题最多的两个控件是ModalPopup和Slider. AjaxControlToolkit的BUG改进显然没有我期望的那么快,最新版本里面是这样说的: Version 1.0.11119.0 for ASP.NET AJAX version 1.0 and .NET Framework 2.0 (No changes from 1.0.10920) 问题总要解决,生活还在继续,既然AjaxControlToolkit差强人意 那就回头看看基于Asp.net Ajax框架我们直接使用脚本可以做什么? 2WbLinux联盟 2WbLinux联盟 翻看Ajax客户端的文档,的确要是看完也需些时日了,不禁想起《神雕侠侣》中小龙女和杨过被李莫愁点穴困于墓室里突然看到《九阴真经》: 她转念又想:“我纵然通了穴道,但打不过师姊,仍是无用。”当即细看室顶经文,要找一门即知即用的武功,一出手就将李莫愁制住,但约略瞥去,每一项皆是艰深繁复,料想就算是最易的功夫,也须数十日方能练成... ...” 2WbLinux联盟 2WbLinux联盟 2WbLinux联盟 是的,就如《九阴真经》,文档有时间一定要好好研习,如果没有足够的时间,最好能沿着一条脉络抽取出自己所需; 2WbLinux联盟 2WbLinux联盟 本文简单描述了: 2WbLinux联盟 1.javascript 如何调用WebService 2WbLinux联盟 2.javascript 如何调用服务器端方法 2WbLinux联盟 3.javascript 如何用POST方式向服务器端提交数据 2WbLinux联盟 4.javascript 如何用Get方式向服务器端提交数据 2WbLinux联盟 2WbLinux联盟 2WbLinux联盟 2WbLinux联盟 1.javascript 如何调用WebService 2WbLinux联盟 服务器端代码: 1 using System; 2WbLinux联盟 2 using System.Collections; 2WbLinux联盟 3 using System.Web; 2WbLinux联盟 4 using System.Web.Services; 2WbLinux联盟 5 using System.Web.Services.Protocols; 2WbLinux联盟 6 using System.Web.Script.Services; 2WbLinux联盟 7 2WbLinux联盟 8 /// <summary> 2WbLinux联盟 9 /// Summary description for WebService 2WbLinux联盟 10 /// </summary> 2WbLinux联盟 11 [WebService(Namespace = "http://tempuri.org/")] 2WbLinux联盟 12 [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 2WbLinux联盟 13 [ScriptService] 2WbLinux联盟 14 public class WebService : System.Web.Services.WebService { 2WbLinux联盟 15 2WbLinux联盟 16 public WebService () { 2WbLinux联盟 17 2WbLinux联盟 18 //Uncomment the following line if using designed components 2WbLinux联盟 19 //InitializeComponent(); 2WbLinux联盟 20 } 2WbLinux联盟 21 2WbLinux联盟 22 [WebMethod] 2WbLinux联盟 23 public string HelloWorld(string user) { 2WbLinux联盟 24 return "<p style='backGround-color:red;'>"+user+ " Hello World!</p>"; 2WbLinux联盟 25 } 2WbLinux联盟 26 2WbLinux联盟 27 } 2WbLinux联盟 28 2WbLinux联盟 29 2WbLinux联盟 客户端代码: 2WbLinux联盟 1 <script type="text/javascript"> 2WbLinux联盟 2 //下面的脚本调用WebService取回数据显示在webserviceDataDiv 2WbLinux联盟 3 function getDataFromWebService() 2WbLinux联盟 4 { 2WbLinux联盟 5 WebService.HelloWorld($get("Text1").value,OnGetDataFromWebServiceCompleted,null,null); 2WbLinux联盟 6 } 2WbLinux联盟 7 function OnGetDataFromWebServiceCompleted(data) 2WbLinux联盟 8 { 2WbLinux联盟 9 $get("webserviceDataDiv").innerHTML=data; 2WbLinux联盟 10 } 2WbLinux联盟 11 </script> 2WbLinux联盟 12 2WbLinux联盟 13 <script type="text/javascript"> 2WbLinux联盟 14 function getDataByServerMethod() 2WbLinux联盟 15 { 2WbLinux联盟 16 PageMethods.ShowDetailData($get('Text1').value,OnGetDataFromWebMethodCompleted); 2WbLinux联盟 17 } 2WbLinux联盟 18 function OnGetDataFromWebMethodCompleted(data) 2WbLinux联盟 19 { 2WbLinux联盟 20 $get("webserviceDataDiv").innerHTML=data; 2WbLinux联盟 21 } 2WbLinux联盟 22 </script> 2WbLinux联盟 23 2WbLinux联盟 24 <input id="Text1" type="text" value="me_sa" /> 2WbLinux联盟 25 <input id="Button3" type="button" value="GetDataFromWebService" onclick="getDataFromWebService();" /> 2WbLinux联盟 26 <div id="webserviceDataDiv"> 2WbLinux联盟 27 </div>如果你使用VS2008你可以得到这样的提示: 2WbLinux联盟 2WbLinux联盟 2.javascript 如何调用服务器端方法 2WbLinux联盟 服务器端代码: 2WbLinux联盟 1 using System; 2WbLinux联盟 2 using System.Configuration; 2WbLinux联盟 3 using System.Data; 2WbLinux联盟 4 using System.Web; 2WbLinux联盟 5 using System.Web.Security; 2WbLinux联盟 6 using System.Web.UI; 2WbLinux联盟 7 using System.Web.UI.HtmlControls; 2WbLinux联盟 8 using System.Web.UI.WebControls; 2WbLinux联盟 9 using System.Web.UI.WebControls.WebParts; 2WbLinux联盟 10 using System.Web.Services; 2WbLinux联盟 11 2WbLinux联盟 12 public partial class _Default : System.Web.UI.Page 2WbLinux联盟 13 { 2WbLinux联盟 14 protected void Page_Load(object sender, EventArgs e) 2WbLinux联盟 15 { 2WbLinux联盟 16 2WbLinux联盟 17 } 2WbLinux联盟 18 2WbLinux联盟 19 [WebMethod] 2WbLinux联盟 20 public static string ShowDetailData(string user) 2WbLinux联盟 21 { 2WbLinux联盟 22 return user+"你好!你知道下面的文字选自哪里么?<br/>"+ "郭靖知道师父虽然摔下,并不碍事,但欧阳锋若乘势追击,后着可凌厉之极,当下叫道:“看招!”左腿微屈,右掌划了个圆圈,平推出去,正是降龙十八掌中的“亢龙有悔”。这一招他日夕勤练不辍,初学时便已非同小可,加上这十余年苦功,实已到炉火纯青之境,初推出去时看似轻描淡写,但一遇阻力,能在刹时之间连加一十三道后劲,一道强似一道,重重叠叠,直是无坚不摧、无强不破。这是他从九阴真经中悟出来的妙境。纵是洪七公当年,单以这一招而论,也无如此精奥的造诣。"; 2WbLinux联盟 23 } 2WbLinux联盟 24 } 2WbLinux联盟 25 客户端代码: 2WbLinux联盟 1 <script type="text/javascript"> 2WbLinux联盟 2 //下面的脚本调用WebService取回数据显示在webserviceDataDiv 2WbLinux联盟 3 2WbLinux联盟 4 <script type="text/javascript"> 2WbLinux联盟 5 function getDataByServerMethod() 2WbLinux联盟 6 { 2WbLinux联盟 7 PageMethods.ShowDetailData($get('Text1').value,OnGetDataFromWebMethodCompleted); 2WbLinux联盟 8 } 2WbLinux联盟 9 function OnGetDataFromWebMethodCompleted(data) 2WbLinux联盟 10 { 2WbLinux联盟 11 $get("webserviceDataDiv").innerHTML=data; 2WbLinux联盟 12 } 2WbLinux联盟 13 </script> 2WbLinux联盟 14 2WbLinux联盟 15 <input id="Text1" type="text" value="me_sa" /> 2WbLinux联盟 16 <input id="Button3" type="button" value="GetDataFromWebService" onclick="getDataFromWebService();" /> 2WbLinux联盟 17 <input id="Button4" type="button" value="getDataByServerMethod" onclick="getDataByServerMethod();" /> 2WbLinux联盟 18 <div id="webserviceDataDiv"> 2WbLinux联盟 19 </div> 2WbLinux联盟 3.javascript 如何用POST方式向服务器端提交数据 4.javascript 如何用Get方式向服务器端提交数据 2WbLinux联盟 2WbLinux联盟 客户端: 2WbLinux联盟 2WbLinux联盟 2WbLinux联盟 1 <script type="text/javascript"> 2WbLinux联盟 2 //下面的代码使用Post和Get两种方式向服务器提交数据 2WbLinux联盟 3 //这里做了一个简单的重构,两种方式调用的时候只要传递HttpVerb就可以了 2WbLinux联盟 4 function getData(verb) 2WbLinux联盟 5 { 2WbLinux联盟 6 var webRequest=new Sys.Net.WebRequest(); 2WbLinux联盟 7 if (verb=="POST") 2WbLinux联盟 8 { 2WbLinux联盟 9 webRequest.set_url("ProcessRequest.aspx"); 2WbLinux联盟 10 } 2WbLinux联盟 11 else 2WbLinux联盟 12 { 2WbLinux联盟 13 webRequest.set_url("ProcessRequest.aspx?AID="+$get("aidTextbox").value); 2WbLinux联盟 14 } 2WbLinux联盟 15 webRequest.add_completed(OnCompleted); 2WbLinux联盟 16 2WbLinux联盟 17 webRequest.set_httpVerb(verb); 2WbLinux联盟 18 var requestBody="AID="+$get("aidTextbox").value; 2WbLinux联盟 19 webRequest.set_body(requestBody); 2WbLinux联盟 20 webRequest.get_headers()["Conten_Length"]=requestBody.length; 2WbLinux联盟 21 webRequest.invoke(); 2WbLinux联盟 22 } 2WbLinux联盟 23 function getDataByPost() 2WbLinux联盟 24 { 2WbLinux联盟 25 getData("POST"); 2WbLinux联盟 26 } 2WbLinux联盟 27 function getDataByGet() 2WbLinux联盟 28 { 2WbLinux联盟 29 getData("Get"); 2WbLinux联盟 30 } 2WbLinux联盟 31 //完成之后调用的参数 注意一下参数 2WbLinux联盟 32 function OnCompleted(executor,eventArgs) 2WbLinux联盟 33 { 2WbLinux联盟 34 if(executor.get_responseAvailable()) 2WbLinux联盟 35 { 2WbLinux联盟 36 $get("data").innerHTML=executor.get_responseData(); 2WbLinux联盟 37 } 2WbLinux联盟 38 } 2WbLinux联盟 39 </script> 2WbLinux联盟 40 2WbLinux联盟 41 <div id="data"> 2WbLinux联盟 42 </div> 2WbLinux联盟 43 <input id="aidTextbox" type="text" value="me_sa" /> 2WbLinux联盟 44 <input id="Button1" type="button" value="Post" onclick="javascript:getDataByPost();" /> 2WbLinux联盟 45 <input id="Button2" type="button" value="Get" onclick="javascript:getDataByGet();" /> 2WbLinux联盟 ProcessRequest.aspx服务器端代码: 2WbLinux联盟 1 using System; 2WbLinux联盟 2 using System.Collections; 2WbLinux联盟 3 using System.Configuration; 2WbLinux联盟 4 using System.Data; 2WbLinux联盟 5 using System.Web; 2WbLinux联盟 6 using System.Web.Security; 2WbLinux联盟 7 using System.Web.UI; 2WbLinux联盟 8 using System.Web.UI.HtmlControls; 2WbLinux联盟 9 using System.Web.UI.WebControls; 2WbLinux联盟 10 using System.Web.UI.WebControls.WebParts; 2WbLinux联盟 11 2WbLinux联盟 12 public partial class ProcessRequest : System.Web.UI.Page 2WbLinux联盟 13 { 2WbLinux联盟 14 protected void Page_Load(object sender, EventArgs e) 2WbLinux联盟 15 { 2WbLinux联盟 16 string temp = ""; 2WbLinux联盟 17 Response.Clear(); 2WbLinux联盟 18 if (!string.IsNullOrEmpty(Request.Form["AID"])) 2WbLinux联盟 19 { 2WbLinux联盟 20 temp =DateTime.Now.ToString()+ " POST过来的数据是:" + Request.Form["AID"].ToString(); 2WbLinux联盟 21 } 2WbLinux联盟 22 if (!string.IsNullOrEmpty(Request.QueryString["AID"])) 2WbLinux联盟 23 { 2WbLinux联盟 24 temp = DateTime.Now.ToString() + "GET过来的数据是:" + Request.QueryString["AID"].ToString(); 2WbLinux联盟 25 } 2WbLinux联盟 26 2WbLinux联盟 27 Response.Write(temp); 2WbLinux联盟 28 Response.End(); 2WbLinux联盟 29 2WbLinux联盟 30 } 2WbLinux联盟 31 } 2WbLinux联盟 32 2WbLinux联盟 就说这么多,代码下载地址:http://www.cnblogs.com/Files/me-sa/AjaxTest.rar 2WbLinux联盟 2WbLinux联盟 2WbLinux联盟 2WbLinux联盟 回头再说,现在开始...... 2WbLinux联盟
Linux联盟收集整理 ,转贴请标明原始链接,如有任何疑问欢迎来本站Linux论坛讨论 |
|
|
|
|
|