前后台互相调用function
最近看到好几个帖子都在问后台如何调用前台?前台如何调用后台?小弟总结了一下share给大家,有不足之处欢迎补充.
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="JavaScript get Net Function.aspx.cs" Inherits="JavaScript_get_Net_Function" %>
<!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>
<%--
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.添加 using System.Web.Services;
--%>
<%--
ASP.Net后臺調用JavaScript function的方法,我們要注意:
1.盡量少用Response.Write(<script></script>);這種方法,它會影響CSS導致頁面效果偏差
2.C#中使用ScriptManager.RegisterStartupScript(this, this.GetType(), "edit", "JS('"+param1+"','"+param2+"')", true);
3.添加 using System.Web.UI;
--%>
<script type="text/javascript">
/*-------------JavaScript調用.Net后臺Start----------------------------------*/
function JsCallCSharp(param1)
{
PageMethods.SayHelloFromPage(param1,onSayHelloSucceeded);
}
function onSayHelloSucceeded(result)
{
alert(result);
}
/*-------------JavaScript調用.Net后臺End----------------------------------*/
/*-------------.Net調用JavaScript Start-----------------------------------*/
function CSharpCallJs(param1,param2)
{
alert(param1 + param2);
}
/*-------------.Net調用JavaScript Start-----------------------------------*/
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
</div>
<asp:Button ID="Button1" onclick="Button1_Click" OnClientClick="JsCallCSharp('takako');" runat="server" Text="Button1" />
</form>
</body>
</html>
--------------------编程问答-------------------- 谢谢分享. --------------------编程问答-------------------- 有BUG? --------------------编程问答--------------------
using System;
using System.Web.Services;
using System.Web.UI;
public partial class JavaScript_get_Net_Function : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string param1 = "你好:";
string param2 = "takako_mu";
ScriptManager.RegisterStartupScript(this, this.GetType(), "edit", "CSharpCallJs('" + param1 + "','" + param2 + "');", true);
}
[WebMethod]
public static string SayHelloFromPage(string name)
{
return string.Format("Hello {0}!", name);
}
}
什么BUG? --------------------编程问答-------------------- 貌似不行,如果再后台弹出你自定义的框时,肯定会只是一闪而过,貌似只能用Ajax好点 --------------------编程问答-------------------- the server method 'SayHelloFromPage' failed with the following error
不行啊,有错误啊 --------------------编程问答-------------------- the server method 'SayHelloFromPage' failed with the following error
不行啊,有错误啊 --------------------编程问答-------------------- [WebMethod]
应该是用了ajaxpro了吧~~~ --------------------编程问答-------------------- 又是<asp:ScriptManager ID="ScriptManager1" EnablePageMethods="true" runat="server" />
--------------------编程问答-------------------- 不错,谢谢!
大力顶! --------------------编程问答-------------------- 好帖子 ,俺收藏了 --------------------编程问答-------------------- 收藏!
感觉不错! --------------------编程问答-------------------- GOOD --------------------编程问答-------------------- 收藏先 --------------------编程问答-------------------- mark!! --------------------编程问答-------------------- 谢楼主!!收藏!! --------------------编程问答-------------------- 试了一下,
前台调用后台还是不行哦!! --------------------编程问答-------------------- 先回帖……后看帖…… --------------------编程问答-------------------- 在浏览器状态栏总出现:"已完毕,但网页上有错误" --------------------编程问答--------------------
可能代碼遺漏了什么,我拿過去試一下告訴你結果 --------------------编程问答-------------------- 。。这样用不稀奇吧。 --------------------编程问答-------------------- 我可以的.... --------------------编程问答-------------------- 我的是VS 2008 --------------------编程问答-------------------- 谢谢总结 --------------------编程问答-------------------- 前台调用后台似乎要点N下才会有一下弹出来…… --------------------编程问答--------------------
我是vs2005不行
我再vs2008下试试!! --------------------编程问答-------------------- 转帖也要转成简体的 --------------------编程问答-------------------- 先看看 --------------------编程问答--------------------
Sorry,不是轉帖.原創. --------------------编程问答-------------------- 后台调用前台?牛 --------------------编程问答-------------------- 不管么样 还是要顶一下.... --------------------编程问答--------------------
the server method 'SayHelloFromPage' failed with the following error
不行啊,有错误啊
原因是:
在Web.config中少了配置项~~~~
<httpModules>
<add name="ScriptModule" type="System.Web.Handlers.ScriptModule, System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</httpModules> --------------------编程问答--------------------
帮顶 --------------------编程问答-------------------- --------------------编程问答-------------------- 马克 --------------------编程问答-------------------- mark --------------------编程问答-------------------- up --------------------编程问答-------------------- UP --------------------编程问答-------------------- 谢谢分享
学习了 --------------------编程问答-------------------- 楼主 你是港台的啊 --------------------编程问答-------------------- 虽然看着很累
但是真的很实用
谢了 --------------------编程问答-------------------- UP... --------------------编程问答-------------------- mark --------------------编程问答-------------------- Good
thanks --------------------编程问答-------------------- !!来点有创意的 行不? --------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- 感觉好像还行咯,回去用用啊,
很实用的哦, --------------------编程问答-------------------- 怎么一点小问题???? --------------------编程问答-------------------- vs2005 报错未定义Sys是什么原因,,报错地方在下面
PageMethods.prototype = {
SayHelloFromPage:function(name,succeededCallback, failedCallback, userContext) {
return this._invoke(PageMethods.get_path(), 'SayHelloFromPage',false,{name:name},succeededCallback,failedCallback,userContext); }}
PageMethods.registerClass('PageMethods',Sys.Net.WebServiceProxy); --------------------编程问答-------------------- 很好,收藏了 --------------------编程问答-------------------- gou niu --------------------编程问答-------------------- 好是好, 可惜是.net的 要是JAVA就好了 --------------------编程问答-------------------- 很好,收藏了 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 2005下面不能使用。 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 学习了 --------------------编程问答-------------------- mark --------------------编程问答-------------------- mark --------------------编程问答-------------------- 學習 --------------------编程问答-------------------- 这明显用了ajax,签定完毕! --------------------编程问答-------------------- mark --------------------编程问答-------------------- very good --------------------编程问答-------------------- 学习了! --------------------编程问答--------------------
好贴 --------------------编程问答-------------------- ajaxpro --------------------编程问答-------------------- 2005下面不能使用。 --------------------编程问答-------------------- 还是Web.config中少了配置项,网上查查吧~~ --------------------编程问答-------------------- up --------------------编程问答--------------------
一个封装的在后台弹出JS Alert消息和JS confirm信息以及跳转到指定的页面
http://www.cnblogs.com/xiaoxijin/archive/2008/03/23/1118172.html
--------------------编程问答-------------------- [WebMethod]
加这个和前台调用后台方法有关系吗?
[AxaxPro.AjaxMethod]
如果是ajax的话加这个。webmethod我只过用在webservice
不知道楼主可否说下如何通过[webmethod]调用
不知道是否我见识少 --------------------编程问答-------------------- [webmethod]我也确实只有在webservice上用过 --------------------编程问答-------------------- 。。。。。。。。。。。。。。。。。。。。。。。。。。。。 --------------------编程问答-------------------- 钅 --------------------编程问答-------------------- UP --------------------编程问答-------------------- mark先 --------------------编程问答--------------------
这个错误是ScriptManager的问题 ,自己在检查下 ,有时项目中,位置放的不对也会报错 --------------------编程问答-------------------- 看看... --------------------编程问答-------------------- 好想法啊 --------------------编程问答-------------------- 原来是AJAX.NET啊,不错呢,AJAX.NET非常好用而且简单,建议大家去www.asp.net去看一下微软的官方入门视频,很简单的! --------------------编程问答-------------------- 顶起..谢谢了..收藏... --------------------编程问答-------------------- 嗯.测试有效.aspx文件需要添加代码
<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" Namespace="System.Web.UI" TagPrefix="asp" %>
web.config也需要添加代码,不然会显示"sys"未定义 --------------------编程问答-------------------- 顶。 --------------------编程问答-------------------- mark --------------------编程问答-------------------- 不错,谢谢分享了。 --------------------编程问答-------------------- 顶 收藏! --------------------编程问答-------------------- 就加个webservers一样的吧
--------------------编程问答-------------------- 就加个webservers一样的吧 --------------------编程问答-------------------- 那还不如用ICallbackEventHandle --------------------编程问答-------------------- MARK@
补充:.NET技术 , ASP.NET