问题描述
我好像知道有两种方法,一种是调用WebService一种是调用CodeBehinde里的方法,将此方法标记为Ajax可调用就行忘记怎么用了,哪位大侠给解释下,谢谢
解决方案
解决方案二:
把Web服务前加一个[System.Web.Script.Services.ScriptService]即可在javascript调用
解决方案三:
jscode:varxmlHttp=null;functionCreateXMLHttpRequest(){if(window.ActiveXObject)xmlHttp=newActiveXObject("Microsoft.XMLHTTP");if(window.XMLHttpRequest)xmlHttp=newXMLHttpRequest();}functionW(){CreateXMLHttpRequest();xmlHttp.open("get","Ajax.aspx",true);xmlHttp.onreadystatechange=XMLHttpChange;xmlHttp.send(null);}functionXMLHttpChange(){}HtmlCode:<bodyonload="W()"></body>然后在Ajax.aspx.cs的load事件中写要处理的代码。
解决方案四:
后台:publicpartialclass_Default:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){//必要的AjaxPro.Utility.RegisterTypeForAjax(typeof(_Default));}[AjaxPro.AjaxMethod]publicDataSetgetNextClass(stringcid){stringsql=@"selectcnameastxt,idasvolfromwebclasswhereparentid="+cid;try{returngetDs(sql);}catch{//throw;returnnull;}}}前台:<script>varsid=1;varv=_Default.getNextClass(sid).value;//类的名称</script>
解决方案五:
呵呵。楼上的,楼主用的是ajax.net而不是ajaxpro哦1楼正解
解决方案六:
具体调用代码能给下吗?还有如果要对前台可视控件进行操作,WebService就不可行了,怎么办呢?
解决方案七:
自己顶下
解决方案八:
放一个隐藏服务器Button控件,添加要实现的事件:.....varxmlHttp;functioncreateXMLRequest(){if(window.ActiveXObject){xmlHttp=newActiveXObject("Microsoft.XMLHTTP");}elseif(window.XMLHttpRequest){xmlHttp=newXMLHttpRequest();}else{//alert("不能创建XmlHttpRequest");}}functionstartRequest(obj){createXMLRequest();xmlHttp.onreadystatechange=handleStateChange;xmlHttp.open("GET","../API/handler/GetClientByMob.ashx?ClientMob="+obj.value,true);xmlHttp.send(null);}functionhandleStateChange(){if(xmlHttp.readyState==4){if(xmlHttp.status==200){vardivMes=document.getElementById("divUserList");divMes.innerHTML=xmlHttp.responseText;}}}functioncheckUser(obj){varuname=document.getElementById(obj.id).parentNode;document.getElementById("HidClientId").value=obj.id;document.getElementById("btn1").click();------------------------调用该事件}
解决方案九:
ls的错误,是AJax.net就是UpdatePanel的那种
解决方案十:
1.JS和.Net前后臺互相調用總結<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="JavaScriptgetNetFunction.aspx.cs"Inherits="JavaScript_get_Net_Function"%><!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><htmlxmlns="http://www.w3.org/1999/xhtml"><headrunat="server"><title>前臺與后臺方法護調總結</title><%--JavaScript異步調用定義在ASP.Net頁面中的方法,我們需要:1.將該方法聲明為公有(public);2.將該方法聲明為類方法(C#中的static,VB.NET中的Shared),而不是實例方法;3.將該方法添加【WebMethod】屬性4.將頁面中ScriptManager控件的EnablePageMethods屬性設置為true;5.在客戶端使用如下JavaScript語法調用該頁面方法PageMethods.[MethodName](param1,param2,...,callbackFunction);6.為客戶端異步調用指定回調函數,在回調函數中接受返回值并進一步處理;7.添加usingSystem.Web.Services;--%><%--ASP.Net后臺調用JavaScriptfunction的方法,我們要注意:1.盡量少用Response.Write(<script></script>);這種方法,它會影響CSS導致頁面效果偏差2.C#中使用ScriptManager.RegisterStartupScript(this,this.GetType(),"edit","JS('"+param1+"','"+param2+"')",true);3.添加usingSystem.Web.UI;--%><scripttype="text/javascript">/*-------------JavaScript調用.Net后臺Start----------------------------------*/functionJsCallCSharp(param1){PageMethods.SayHelloFromPage(param1,onSayHelloSucceeded);}functiononSayHelloSucceeded(result){alert(result);}/*-------------JavaScript調用.Net后臺End----------------------------------*//*-------------.Net調用JavaScriptStart-----------------------------------*/functionCSharpCallJs(param1,param2){alert(param1+param2);}/*-------------.Net調用JavaScriptStart-----------------------------------*/</script></head><body><formid="form1"runat="server"><div><asp:ScriptManagerID="ScriptManager1"EnablePageMethods="true"runat="server"/></div><asp:ButtonID="Button1"onclick="Button1_Click"OnClientClick="JsCallCSharp('takako');"runat="server"Text="Button1"/></form></body></html>
usingSystem;usingSystem.Web.Services;usingSystem.Web.UI;publicpartialclassJavaScript_get_Net_Function:System.Web.UI.Page{protectedvoidPage_Load(objectsender,EventArgse){}protectedvoidButton1_Click(objectsender,EventArgse){stringparam1="你好:";stringparam2="takako_mu";ScriptManager.RegisterStartupScript(this,this.GetType(),"edit","CSharpCallJs('"+param1+"','"+param2+"');",true);}[WebMethod]publicstaticstringSayHelloFromPage(stringname){returnstring.Format("Hello{0}!",name);}}
解决方案十一:
2.Js調用WebService總結/*--相當于GridViewBind()-------------------------------------------------------------------------------------------*/functionSearch(pagesize,pageCurrentCount){varUserName=$get("optSelectUserName").value;varProject=$get("optSelectProject").value;GetData(pagesize,pageCurrentCount,UserName,Project);returnfalse;}functionGetData(pagesize,pageCurrentCount,userName,project){$get("displayImage").style.display="block";varUserName=userName;varProject=project;WebService_AJAXGridView.GetData(pagesize,pageCurrentCount,UserName,Project,SucceededCallback,FailedCallback,"XmlDocument");}functionSucceededCallback(result,userContext,methodName){if(userContext=="XmlDocument"){UpdateTable(result);}$get("displayImage").style.display="none";//document.getElementById("lab_TotalPage").innerText=}functionFailedCallback(error){//Displaytheerror.varstackTrace=error.get_stackTrace();varmessage=error.get_message();varstatusCode=error.get_statusCode();varexceptionType=error.get_exceptionType();vartimeout=error.get_timedOut();varerrstr="推疊追蹤:"+stackTrace+"n";errstr+="服務錯誤:"+message+"n";errstr+="狀態碼:"+statusCode+"n";errstr+="例外類型:"+exceptionType+"n";errstr+="逾時:"+timeout+"n";errstr+="請關掉IE,重新進入!!";alert(errstr);$get("displayImage").style.display="none";}/*--相當于GridViewBind()-------------------------------------------------------------------------------------------*/
//WebServiceusingSystem;usingSystem.Data;usingSystem.Xml;usingSystem.Text;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Services;usingSystem.Web.Services.Protocols;usingSystem.Web.Script.Services;usingSystem.Web.Script.Serialization;usingSystem.Web.Security;usingSystem.Xml.Linq;usingSystem.Data.Sql;usingSystem.Data.SqlClient;///<summary>///WebService_AJAXGridView的摘要描述///</summary>[WebService(Namespace="http://tempuri.org/")][WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]//若要允許使用ASP.NETAJAX從指令碼呼叫此Web服務,請取消註解下一行。[System.Web.Script.Services.ScriptService]publicclassWebService_AJAXGridView:System.Web.Services.WebService{publicWebService_AJAXGridView(){//如果使用設計的元件,請取消註解下行程式碼//InitializeComponent();}//CreateByTakako_Yang,at2008/12/04[WebMethod(Description="MyAJAXGridviewDataSelect",EnableSession=true)][ScriptMethod(ResponseFormat=ResponseFormat.Xml)]publicXmlDocumentGetData(intpagesize,intpageCurrentCount,stringuserName,stringproject){DailyWorkFactorymyDailyWorkFactory=newDailyWorkFactory();DataTabledt=myDailyWorkFactory.GetDailyWork(0,1,pagesize,pageCurrentCount,userName,project,"");//flag1用來判斷Select來源自現有的,還是編輯時Create的新的StringBuildersb=newStringBuilder();sb.Append("<AJAXSet>");//select[No],Project,Item,Code,[Description(purpose/value)],//Status,[%],[StartDate],[DueDate],Owner,[Day/Hour]fromdaily_workforeach(DataRowmyDataRowindt.Rows){stringNo=myDataRow[0].ToString();stringProject1=myDataRow[1].ToString();stringItem=myDataRow[2].ToString();stringCode=myDataRow[3].ToString();stringDesciption=myDataRow[4].ToString();stringStatus=myDataRow[5].ToString();stringPercent=myDataRow[6].ToString();stringStartDate=(Convert.ToDateTime(myDataRow[7])).ToString("yyyy/MM/dd");stringDueDate=(Convert.ToDateTime(myDataRow[8])).ToString("yyyy/MM/dd");stringOwner=myDataRow[9].ToString();stringNeedTime=myDataRow[10].ToString();sb.Append("<AJAXData>");sb.Append("<P1>").Append(No).Append("</P1>");sb.Append("<P2>").Append(Project1).Append("</P2>");sb.Append("<P3>").Append(Item).Append("</P3>");sb.Append("<P4>").Append(Code).Append("</P4>");sb.Append("<P5>").Append(Desciption).Append("</P5>");sb.Append("<P6>").Append(Status).Append("</P6>");sb.Append("<P7>").Append(Percent).Append("</P7>");sb.Append("<P8>").Append(StartDate).Append("</P8>");sb.Append("<P9>").Append(DueDate).Append("</P9>");sb.Append("<P10>").Append(Owner).Append("</P10>");sb.Append("<P11>").Append(NeedTime).Append("</P11>");sb.Append("</AJAXData>");}sb.Append("</AJAXSet>");XmlDocumentxmlDoc=newXmlDocument();xmlDoc.LoadXml(sb.ToString());returnxmlDoc;}