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. BZOJ 4399: 魔法少女LJJ 线段树合并 + 对数

    Description 在森林中见过会动的树,在沙漠中见过会动的仙人掌过后,魔法少女LJJ已经觉得自己见过世界上的所有稀奇古怪的事情了LJJ感叹道“这里真是个迷人的绿色世界,空气清新.淡雅,到处散发着 ...

  2. window.location.hash(hash应用)---跳转到hash值制定的具体页面

    location是javascript里边管理地址栏的内置对象,比如location.href就管理页面的url,用location.href=url就可以直接将页面重定向url.而location. ...

  3. python3学习笔记(三):注释和字符串

    一.注释 为了让别人能够更容易理解程序,使用注释是非常有效的,即使是自己回头再看旧代码也是一样. # 打印圆的周长: print(2* pi* r) 在python 中用井号(#)表示注释.井号(#) ...

  4. [THUSC2017]大魔法师:线段树

    分析 在线段树上用\(4 \times 4\)的矩阵打标记. 代码 #include <bits/stdc++.h> #define rin(i,a,b) for(register int ...

  5. Spring Boot教程(十四)快速入门

    快速入门 本章主要目标完成Spring Boot基础项目的构建,并且实现一个简单的Http请求处理,通过这个例子对Spring Boot有一个初步的了解,并体验其结构简单.开发快速的特性. 系统要求: ...

  6. jieba (中文词频统计) 、collections (字频统计)、WordCloud (词云)

    py库: jieba (中文词频统计) .collections (字频统计).WordCloud (词云) 先来个最简单的: # 查找列表中出现次数最多的值 ls = [1, 2, 3, 4, 5, ...

  7. linux上的常用的系统自带命令

    wikipedia 发现的分类,发现还有好多没用过.. Unix command-line interface programs and shell builtins   File system ca ...

  8. leetcode-mid-Linked list- 200. Number of Islands¶

    mycode  57.92% class Solution(object): def numIslands(self, grid): """ :type grid: Li ...

  9. bind标签_databaseId标签,_parameter标签的使用

    1.在接口写方法 public List<Employee> getEmpsTestInnerParameter(Employee employee); 2在映射文件中进行配置 <s ...

  10. Mybaits查询返回值是List类型的

    查询返回值是list类型的 1 首先在接口中写方法 public interface EmployeeMapper { public List<Employee> getEmpsByLas ...