使用ScriptManager服务器控件前后台数据交互
2024-10-01 01:48:08
前台页面信息:
<%@ 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方法的任意内容。
最新文章
- C# Azure 存储-Blob
- JS弹出浮层
- (转)java缓存技术,记录
- 在Abp中执行sql语句
- ZOJ 3494 (AC自动机+高精度数位DP)
- BZOJ3206 [Apio2013]道路费用
- 【HTML】Advanced6:HTML5 Forms Pt. 1: Input Types
- NSTimer你真的会用了吗
- 利用libcurl进行post
- C++ - 复制容器(container)的元素至还有一个容器
- Android OpenGL ES 入门系列(二) --- 环境搭建
- wdcp lanmp 安装+搭建网站+安全狗安装 详细实用
- Selenium基础知识(九)验证码
- django面试题必问
- 做为一个.net码农,打开公司的一个项目,大叔我哭了
- Spark的运行模式(1)--Local和Standalone
- 用MyEclipse JPA创建项目(一)
- 调整Linux的最大文件打开数
- php static 变量用法
- mysql 数据操作 单表查询 where约束 练习
热门文章
- systemtap --diskio
- eclipse的Java项目修改后要不要重启tomcat问题
- Python实例--C#执行Python脚本,传参
- [Vue-rx] Share RxJS Streams to Avoid Multiple Requests in Vue.js
- HDU 5358 多校第6场 First One
- PLY格式文件具体解释
- Android图形显示系统——上层显示1:界面绘制大纲
- 浏览器的多线程和js的单线程--前端易混淆知识科普(一)
- URAL 1326. Bottle Taps(简单的状压dp)
- springmvc的执行流程详解