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方法的任意内容。

  • 相关阅读:
    如何快速建立自己的知识体系
    让你的网站变成灰色
    Java多线程-锁升级
    冰河正则大全
    Win硬盘/U盘设置图片
    MySQL——三范式
    MySQL——事务
    MySQL_explain
    HDFS的一些常用指令
    Hadoop集群的搭建准备
  • 原文地址:https://www.cnblogs.com/yuanyanyan/p/6531608.html
Copyright © 2011-2022 走看看