EXT.NET_X.Js.Call方法

本文介绍了如何使用X.Js.Call方法在客户端脚本中传递服务器端变量,并通过实例展示了如何实现服务器端到客户端的变量传递及控制页面元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我们对在前台触发一个客户端事件,来操作界面元素,已经很熟悉,但这属于静态脚本范畴。往往有些时候,我们还需要动态脚本,也就是说,脚本在执行过程中需要的变量,是从服务器端获得的,那么X.Js.Call方法的作用,就是将服务器端变量传给客户端脚本,并执行脚本。

例子

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="AM.Web.Pages.Test" %> <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %> <!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> <script type="text/javascript"> var myAlert = function() { alert("警告"); } var myAlert2 = function(id, val) { var txtField = Ext.getCmp(id); txtField.setValue(val); alert(txtField.getValue()); } </script> </head> <body> <form id="form1" runat="server"> <ext:ResourceManager ID="ResourceManager1" runat="server" /> <ext:Button ID="Button1" runat="server" OnDirectClick="Button1_Click" Text="X.Js.Call一个参数的"> </ext:Button> <ext:Button ID="Button2" runat="server" OnDirectClick="Button2_Click" Text="X.Js.Call两个参数的"> </ext:Button> <ext:TextField ID="TextField1" runat="server" Text=""> </ext:TextField> </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 Ext.Net; namespace AM.Web.Pages { public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, DirectEventArgs e) { X.Js.Call("myAlert"); } protected void Button2_Click(object sender, DirectEventArgs e) { string[] paras = new string[] { this.TextField1.ClientID, "警告" }; X.Js.Call("myAlert2", paras); } } }

1,Button2将服务器端变量传给脚本方法“myAlert2”,脚本根据这个值设置页面控件,并显示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值