zoukankan      html  css  js  c++  java
  • 使用ScriptManager服务器控件前后台数据交互

    前台页面信息:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="QianBao.Dictionary.WebForm1" %>
    
    <!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>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            </asp:ScriptManager>
            <script type="text/javascript">
                function tips() {
                    var txt = document.getElementById("TextBox1").value;
                    PageMethods.aa(txt, funRight);
    
                }
                function funRight(val)       //回调函数,用val接受后台代码aa的执行结果    
                {
                    alert(val);
                }  
            </script>
            <asp:Label ID="Label1" runat="server" Text="请输入信息:"></asp:Label>
            <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            <asp:Button ID="Button1" runat="server" Text="提交" OnClientClick="tips()" />
        </div>
        </form>
    </body>
    </html>

    后台代码

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    
    using System.Web.Services;
    
    namespace QianBao.Dictionary
    {
        public partial class WebForm1 : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
    
            }
            [WebMethod]
            public static string aa(string str )
            {
                return "您输入的是:" + str;
            }
        }
    }

    注:前端页面需要引用ScriptManager服务器控件,并且将EnablePageMethods设置为true

     PageMethods方法

    PageMethods.FunctionName(Parameter1,Parameter2...,funRight,funError,userContext);

    parameter是Function的参数 object、Array类型

    funRight是方法调用成功的回调函数,对返回值进行处理

    funError是后台方法报错后,调用js处理

    userContext是可以传递给SuccessMethod方法,或是FailedMethod方法的任意内容。

  • 相关阅读:
    洛谷1012 拼数
    洛谷1012 拼数
    洛谷 1155 (NOIp2008)双栈排序——仔细分析不合法的条件
    bzoj 3566 [SHOI2014]概率充电器——树型
    bzoj 1415 [Noi2005]聪聪和可可——其实无环的图上概率
    洛谷 1291 [SHOI2002]百事世界杯之旅
    洛谷 1365 WJMZBMR打osu! / Easy
    洛谷 1297 [国家集训队]单选错位——期望
    洛谷 1099 ( bzoj 1999 ) [Noip2007]Core树网的核
    洛谷 2827 蚯蚓——相邻两个比较的分析
  • 原文地址:https://www.cnblogs.com/yuanyanyan/p/6531608.html
Copyright © 2011-2022 走看看