http://www.cnblogs.com/Ren_Lei/archive/2010/07/14/1777413.html

JS调用PageMethods

操作步骤:

1。新建一个WebApplication项目,取默认设置。

2。双击设计界面,进入后天代码编辑界面,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System;
using System.Web.Services;
 
namespace WebApplication1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
        [WebMethod]
        public static string TestFunc1()
        {
            return "This is the returns of behind code function one";
        }
        [WebMethod]
        public static string TestFunc2(string str)
        {
            return "This is the returns of behind code function two!You give me the string :"+str;
        }
    }
}

3。准备好PageMethods后,编辑Default.aspx文件。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
 
<!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>JS调用后台PageMethods</title>
    <script type="text/javascript" language="javascript">
    function TheJSTestfunc1()
    {
        PageMethods.TestFunc1(onsuccess);
    }
    function TheJSTestfunc2()
    {
        var txt=document.getElementById('Text1').value;
        PageMethods.TestFunc2(txt,onsuccess);
    }
    function onsuccess(value)
    {  
    document.getElementById('divResult').innerHTML=value;
    }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
    </asp:ScriptManager>
    <div>
        <input id="Button1" type="button" value="button" onclick="TheJSTestfunc1()" />
        <hr />
        <input id="Text1" type="text" /><input id="Button2" type="button" value="button" onclick="TheJSTestfunc2()" />
        <hr />
        The returns :<div id="divResult"></div>
    </div>
    </form>
</body>
</html>

操作说明:

其实与我的另一篇随笔[JS调用WebService]里面提到的相似。

1.页面中需要添加ScriptManager组件,然后将它的EnablePageMethods属性设置为True。

2.编写页面方法时也要向调用WebService一样,在方法前加[WebMethod],调用WebService与PageMethods其实是同出一辙,原理都是一样的。

JS调用PageMethods的更多相关文章

  1. JS调用asp.net后台方法:PageMethods

    先帮朋友宣传一下程序人生(http://www.manong123.com)的网站,里面都是开发感悟,开发人员创业,支持一下吧~ 原来是通过PageMethods来实现的. 举个列子: Default ...

  2. 关于datagrid中控件利用js调用后台方法事件的问题

    前台调用后台方法除了用button的click事件,还可以用js调用  一.前台页面如图 需求点击这个按钮触发后台事件,从而能够调用存储过程 <epoint:HyperLinkColumn He ...

  3. JS调用Android、Ios原生控件

    在上一篇博客中已经和大家聊了,关于JS与Android.Ios原生控件之间相互通信的详细代码实现,今天我们一起聊一下JS调用Android.Ios通信的相同点和不同点,以便帮助我们在进行混合式开发时, ...

  4. 如何通过JS调用某段SQL语句

    如何通过JS调用某段SQL语句,这样的需求在报表.数据平台开发中很常见.以报表平台FineReport开发为例,例如在点击某个按钮之后,来判断一下数据库条数,再决定下一步操作.那这在后台如何实现呢? ...

  5. iOS开发--JS调用原生OC篇

    JS调用原生OC篇 方式一(反正我不用) 第一种方式是用JS发起一个假的URL请求,然后利用UIWebView的代理方法拦截这次请求,然后再做相应的处理. 我写了一个简单的HTML网页和一个btn点击 ...

  6. 通过js调用android原生方法

    有时候我们有这样一个需求,监听html中控件的一些事件.例如点击html中某个按钮,跳转到别的activity,复制某段文本. 首先是对webview的设置: myWebView = (WebView ...

  7. js调用php和php调用js的方法举例

    js调用php和php调用js的方法举例1 JS方式调用PHP文件并取得php中的值 举一个简单的例子来说明: 如在页面a.html中用下面这句调用: <script type="te ...

  8. android webView开发之js调用java代码示例

    1.webView设置 webView.getSettings().setJavaScriptEnabled(true);//设置支持js webView.addJavascriptInterface ...

  9. c#JS调用

    using MSScriptControl; using System; using System.Collections.Generic; using System.Reflection; usin ...

随机推荐

  1. select * from (XXXXX)[字符]——写法解析

    步骤:1.先执行括号里的语句:查询 select id from three ,将查询出来的数据作为一个结果集 取名为 a2.然后 再 select * from a 查询a ,将 结果集a 全部查询 ...

  2. 「PKUSC 2018」真实排名

    题目链接 戳我 \(Solution\) 我们将现在所要进行的数设为\(now\) 我们分情况讨论一下 他自己不翻倍 他自己翻倍 我们首先来看看\(1\)操作 如果要满足他对排名没有影响,那么不能进行 ...

  3. c++中的类(构造函数,析构函数的执行顺序)

    类对象的初始化顺序 新对象的生成经历初始化阶段(初始化列表显式或者隐式的完成<这部分有点像java里面的初始化块>)——> 构造函数体赋值两个阶段 1,类对象初始化的顺序(对于没有父 ...

  4. 9.并发编程--ThreadLocal

    并发编程--ThreadLocal 1. ThreadLocal : * 线程局部变量,是一种多个线程间并发访问变量的解决方案. * 与其使用synchronized等加锁的方式,ThreadLoca ...

  5. JDK源码--HashMap(之resize)

    1.HashMap源码阅读目标了解具体的数据结构(hash及冲突链表.红黑树)和重要方法的具体实现(hashCode.equals.put.resize...) 2.重要方法 hashCode 与 e ...

  6. LeetCode_509.斐波那契数

    LeetCode-cn_509 509.斐波那契数 斐波那契数,通常用 F(n) 表示,形成的序列称为斐波那契数列.该数列由 0 和 1 开始,后面的每一项数字都是前面两项数字的和.也就是: F(0) ...

  7. Windows Server系统定时任务备份ORACLE数据库

    Windows Server系统定时任务备份ORACLE数据库 一.编辑备份脚本 RMAN备份数据库 1.在备份脚本目录下,创建bat文件db_rman.bat set ORACLE_SID=orcl ...

  8. 二、启动一款app演示

    一.下载aapt包 1. aapt即Android Asset Packaging Tool,在SDK的build-tools目录下.该工具可以查看apk包名和launcherActivity 2.打 ...

  9. MYSQL5.6源码包编译安装

    linux下用cmake编译安装mysql-5.6.35cmake编译安装mysql的方法:#useradd -M mysql -s /sbin/nologin#yum install -y cmak ...

  10. 你知道 Git 是如何做版本控制的吗?(转)

    总结:阅读这篇文章需要20分钟 本文是转载自 滴滴WebApp架构组 的一篇文章,文章讲解了神秘的.git目录下的一些文件,最终阐述了git是如何存储数据,及git分支的相关内容. git如何存储数据 ...