『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复

本文介绍如何使用ExtJS前端框架与ASP.NET后端进行数据交互,包括表单提交、响应处理及参数传递等关键步骤。

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

几点说明


  1. 这里所谓的Asp.NET后台,是指Asp.NET页面的aspx.cs文件的代码
  2. 请不要直接复制代码,由于各人环境不同,可能会产生异常
  3. 本篇中的代码说明中,将省略之前文章中所描述过的内容
  4. 下一篇记录如何实现表单控件事件的侦听

 

 

 

最简单的提交与后台消息回复


IDE: VS2010 SP1

ExtJS版本:3.4.0

 

<!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>
    <title>『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复</title>
    <!-- Common Libs -->
    <link href="../Common/ext-all.css" rel="stylesheet" type="text/css" />
    <script src="../Common/ext-base-debug.js" type="text/javascript"></script>
    <script src="../Common/ext-all-debug-w-comments.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
    Ext.onReady(function () {
        var frm = new Ext.FormPanel({
            renderTo: document.body,
            title: '『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复 —— http://www.cnblogs.com/sitemanager/',
            url: '../jsonresponse.aspx',
            autoWidth: 'true',
            buttons: [{
                text: 'Save',
                handler: function () {
                    frm.getForm().submit({
                        success: function (f, a) {
                            Ext.Msg.alert('Success', 'It worked');
                        },
                        failure: function (f, a) {
                            Ext.Msg.alert('Warning', 'Error');
                        }
                    });
                }
            }, {
                text: 'Reset',
                handler: function () {
                    frm.getForm().reset();
                }
            }]
        });
    });
</script>
</body>
</html>


using System;

namespace csdemo.extjs
{
    public partial class jsonresponse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Write("{success: true}");
        }
    }
}

说明:

  1. buttons 配置项是用于配置表单按钮的,之后的操作都是利用表单按钮的handler来进行的
  2. frm.getForm().submit() 与 frm.getForm.reset() 分别对应于Html表单的submit与reset操作
  3. 使用getForm用于获取对应表单
  4. success 与 failure 分别对应表单提交的两个状态,其后可跟函数,我这里放的是匿名函数。

 

 

 

效果图

image

 

 

 

 

 

 

带有回执消息的实现


<!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>
    <title>『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复</title>
    <!-- Common Libs -->
    <link href="../Common/ext-all.css" rel="stylesheet" type="text/css" />
    <script src="../Common/ext-base-debug.js" type="text/javascript"></script>
    <script src="../Common/ext-all-debug-w-comments.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
    Ext.onReady(function () {
        // Provides attractive and customizable tooltips for any element. 
        Ext.QuickTips.init();

        var classesStore = new Ext.data.SimpleStore({
            fields: ['id', 'class'],
            data: [['0', 'ExtJS 3.3.0'], ['1', 'ExtJS 3.4.0'], ['2', 'ExtJS 4.0']]
        });

        var frm = new Ext.FormPanel({
            renderTo: document.body,
            title: '『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复 —— http://www.cnblogs.com/sitemanager/',
            url: '../jsonresponse.aspx',
            autoWidth: 'true',
            buttons: [{
                text: 'Save',
                handler: function () {
                    frm.getForm().submit({
                        success: function (form, action) {

                            frm.getForm().findField('title').setValue(action.result.title);
                            frm.getForm().findField('author').setValue(action.result.author);
                            frm.getForm().findField('email').setValue(action.result.email);
                            frm.getForm().findField('site').setValue(action.result.site);
                            frm.getForm().findField('contentEssay').setValue(action.result.contentEssay);

                            Ext.Msg.alert('Success', 'Loaded all!');
                        },
                        failure: function (form, action) {
                            Ext.Msg.alert('Warning', action.result.errorMsg);
                        }
                    });
                }
            }, {
                text: 'Reset',
                handler: function () {
                    frm.getForm().reset();
                }
            }],
            items: [{
                xtype: 'textfield',
                fieldLabel: '文章标题',
                name: 'title',
                // Specify false to validate that the value's length is > 0 (defaults to true)
                allowBlank: 'false',
                width: 300,
                // A config object containing one or more event handlers to be added to this object during initialization.
                listeners: {
                    specialkey: function (f, e) {
                        if (e.getKey() == e.ENTER) {
                            Ext.Msg.alert('Listen Success!', e.getKey);
                        }
                    }
                }
            }, {
                xtype: 'textfield',
                fieldLabel: '作者',
                name: 'author',
                width: 300
            }, {
                xtype: 'textfield',
                fieldLabel: '邮箱',
                name: 'email',
                // The error text to display when the email validation function returns false. 
                vtype: 'emailText',
                allowBlank: 'true',
                width: 300
            }, {
                xtype: 'textfield',
                fieldLabel: '主页',
                name: 'site',
                // The error text to display when the url validation function returns false. 
                vtype: 'urlText',
                allowBlank: 'true',
                width: 300
            }, {
                xtype: 'numberfield',
                fieldLabel: '发布次数',
                name: 'publishNumber',
                width: 300
            }, {
                xtype: 'combo',
                fieldLabel: '文章分类',
                name: 'class',
                /* Acceptable values are: 
                *      'remote' : Default 
                *          Automatically loads the store the first time the trigger is clicked. If you do not want the store to be automatically loaded the first time the trigger is clicked, set to 'local' and manually load the store. To force a requery of the store every time the trigger is clicked see lastQuery.
                *      'local' : 
                *          ComboBox loads local data
                */
                mode: 'local',
                // The data source to which this combo is bound (defaults to undefined).
                store: classesStore,
                // The underlying data field name to bind to this ComboBox
                displayField: 'class',
                width: 300
            }, {
                xtype: 'datefield',
                fieldLabel: '发布日期',
                name: 'publishDate',
                // An array of days to disable, 0 based (defaults to null).
                // disable Sunday and Saturday:
                disabledDays: [0, 6],
                width: 300
            }, {
                xtype: 'timefield',
                fieldLabel: '发布时间',
                increment: 15,
                name: 'publishTime',
                width: 300
            }, {
                xtype: 'radio',
                boxLabel: '随笔',
                fieldLabel: '文章类型',
                // Radio grouping is handled automatically by the browser if you give each radio in a group the same name.
                name: 'essayClass',
                width: 300
            }, {
                xtype: 'radio',
                boxLabel: '文章',
                name: 'essayClass',
                width: 300
            }, {
                xtype: 'checkbox',
                fieldLabel: '发布到首页',
                name: 'publishToSiteHome',
                width: 300
            }, {
                xtype: 'textarea',
                fieldLabel: '摘要',
                name: 'abstractEssay',
                width: 500,
                allowBlank: 'true',
                autoScroll: 'true',
                height: 30
            }, {
                // Provides a lightweight HTML Editor component.
                xtype: 'htmleditor',
                fieldLabel: '正文',
                name: 'contentEssay',
                width: 500,
                allowBlank: 'false',
                autoScroll: 'true',
                height: 300
            }]
        });
    });
</script>
</body>
</html>


using System;
using System.Web.Script.Serialization;

namespace csdemo.extjs
{
    public partial class jsonresponse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int resMode;
            JavaScriptSerializer js = new JavaScriptSerializer();
            responseMsg resMsg = new responseMsg();

            resMode = 2;

            switch (resMode)
            {
                case 1:
                    resMsg.success = false;
                    resMsg.errorMsg = "Test the Error Message";
                    Response.Write(js.Serialize(resMsg));
                    break;
                case 2:
                    blogEssay bl = new blogEssay();
                    bl.success = true;
                    bl.title = "『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复";
                    bl.author = "Aaron";
                    bl.contentEssay = "test";
                    bl.site = "http://www.cnblogs.com/sitemanager/";
                    Response.Write(js.Serialize(bl));
                    break;
                default:
                    resMsg.success = false;
                    resMsg.errorMsg = "请先传入参数!";
                    Response.Write(js.Serialize(resMsg));
                    break;
            }
        }
    }

    public class responseMsg
    {
        private bool _success;

        public bool success
        {
            get { return _success; }
            set { _success = value; }
        }
        private string _errorMsg;

        public string errorMsg
        {
            get { return _errorMsg; }
            set { _errorMsg = value; }
        }
    }

    public class blogEssay : responseMsg
    {
        private string _title;

        public string title
        {
            get { return _title; }
            set { _title = value; }
        }
        private string _author;

        public string author
        {
            get { return _author; }
            set { _author = value; }
        }
        private string _email;

        public string email
        {
            get { return _email; }
            set { _email = value; }
        }
        private string _site;

        public string site
        {
            get { return _site; }
            set { _site = value; }
        }
        private int _publishNumber;

        public int publishNumber
        {
            get { return _publishNumber; }
            set { _publishNumber = value; }
        }
        private string _abstractEssay;

        public string abstractEssay
        {
            get { return _abstractEssay; }
            set { _abstractEssay = value; }
        }
        private DateTime _publishDate;

        public DateTime publishDate
        {
            get { return _publishDate; }
            set { _publishDate = value; }
        }
        private string _contentEssay;

        public string contentEssay
        {
            get { return _contentEssay; }
            set { _contentEssay = value; }
        }
    }
}

说明:

  1. 使用getForm().findField(‘表单内控件的name属性').setValue(想要设置的值)
  2. a.result 代表后台回传来的结果集
  3. a.result.xxxx 表示结果集中的某个指定的JSON数据项名的值
  4. 使用JavaScriptSerializer对象来序列化相应类型为JSON数据格式,用于传回给前台

 

效果图

image

 

 

 

 

前台向后台传参数


<!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>
    <title>『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复</title>
    <!-- Common Libs -->
    <link href="../Common/ext-all.css" rel="stylesheet" type="text/css" />
    <script src="../Common/ext-base-debug.js" type="text/javascript"></script>
    <script src="../Common/ext-all-debug-w-comments.js" type="text/javascript"></script>
</head>
<body>
<script type="text/javascript">
    Ext.onReady(function () {
        // Provides attractive and customizable tooltips for any element. 
        Ext.QuickTips.init();

        var classesStore = new Ext.data.SimpleStore({
            fields: ['id', 'class'],
            data: [['0', 'ExtJS 3.3.0'], ['1', 'ExtJS 3.4.0'], ['2', 'ExtJS 4.0']]
        });

        var frm = new Ext.FormPanel({
            renderTo: document.body,
            title: '『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复 —— http://www.cnblogs.com/sitemanager/',
            url: '../jsonresponse.aspx',
            autoWidth: 'true',
            buttons: [{
                text: '加载表单',
                handler: function () {
                    frm.getForm().load({
                        url: '../jsonresponse.aspx',
                        params: {
                            resMode: 3
                        }
                    });
                }
            }, {
                text: '参数互传',
                handler: function () {
                    frm.getForm().submit({
                        url: '../jsonresponse.aspx',
                        params: {
                            resMode: 4
                        },
                        success: function (form, action) {
                            Ext.Msg.alert("Success", action.result.errorMsg);
                        },
                        failure: function (form, action) {
                            Ext.Msg.alert('Warning', action.result.errorMsg);
                        }
                    });
                }
            }, {
                text: 'Save',
                handler: function () {
                    frm.getForm().submit({
                        url: '../jsonresponse.aspx',
                        params: {
                            resMode: 1
                        },
                        success: function (form, action) {

                            frm.getForm().findField('title').setValue(action.result.title);
                            frm.getForm().findField('author').setValue(action.result.author);
                            frm.getForm().findField('email').setValue(action.result.email);
                            frm.getForm().findField('site').setValue(action.result.site);
                            frm.getForm().findField('contentEssay').setValue(action.result.contentEssay);

                            Ext.Msg.alert('Success', 'Loaded all!');
                        },
                        failure: function (form, action) {
                            Ext.Msg.alert('Warning', action.result.errorMsg);
                        }
                    });
                }
            }, {
                text: 'Reset',
                handler: function () {
                    frm.getForm().reset();
                }
            }],
            items: [{
                xtype: 'textfield',
                fieldLabel: '文章标题',
                name: 'title',
                // Specify false to validate that the value's length is > 0 (defaults to true)
                allowBlank: 'false',
                width: 300,
                // A config object containing one or more event handlers to be added to this object during initialization.
                listeners: {
                    specialkey: function (f, e) {
                        if (e.getKey() == e.ENTER) {
                            Ext.Msg.alert('Listen Success!', e.getKey);
                        }
                    }
                }
            }, {
                xtype: 'textfield',
                fieldLabel: '作者',
                name: 'author',
                width: 300
            }, {
                xtype: 'textfield',
                fieldLabel: '邮箱',
                name: 'email',
                // The error text to display when the email validation function returns false. 
                vtype: 'emailText',
                allowBlank: 'true',
                width: 300
            }, {
                xtype: 'textfield',
                fieldLabel: '主页',
                name: 'site',
                // The error text to display when the url validation function returns false. 
                vtype: 'urlText',
                allowBlank: 'true',
                width: 300
            }, {
                xtype: 'numberfield',
                fieldLabel: '发布次数',
                name: 'publishNumber',
                width: 300
            }, {
                xtype: 'combo',
                fieldLabel: '文章分类',
                name: 'class',
                /* Acceptable values are: 
                *      'remote' : Default 
                *          Automatically loads the store the first time the trigger is clicked. If you do not want the store to be automatically loaded the first time the trigger is clicked, set to 'local' and manually load the store. To force a requery of the store every time the trigger is clicked see lastQuery.
                *      'local' : 
                *          ComboBox loads local data
                */
                mode: 'local',
                // The data source to which this combo is bound (defaults to undefined).
                store: classesStore,
                // The underlying data field name to bind to this ComboBox
                displayField: 'class',
                width: 300
            }, {
                xtype: 'datefield',
                fieldLabel: '发布日期',
                name: 'publishDate',
                // An array of days to disable, 0 based (defaults to null).
                // disable Sunday and Saturday:
                disabledDays: [0, 6],
                width: 300
            }, {
                xtype: 'timefield',
                fieldLabel: '发布时间',
                increment: 15,
                name: 'publishTime',
                width: 300
            }, {
                xtype: 'radio',
                boxLabel: '随笔',
                fieldLabel: '文章类型',
                // Radio grouping is handled automatically by the browser if you give each radio in a group the same name.
                name: 'essayClass',
                width: 300
            }, {
                xtype: 'radio',
                boxLabel: '文章',
                name: 'essayClass',
                width: 300
            }, {
                xtype: 'checkbox',
                fieldLabel: '发布到首页',
                name: 'publishToSiteHome',
                width: 300
            }, {
                xtype: 'textarea',
                fieldLabel: '摘要',
                name: 'abstractEssay',
                width: 500,
                allowBlank: 'true',
                autoScroll: 'true',
                height: 30
            }, {
                // Provides a lightweight HTML Editor component.
                xtype: 'htmleditor',
                fieldLabel: '正文',
                name: 'contentEssay',
                width: 500,
                allowBlank: 'false',
                autoScroll: 'true',
                height: 300
            }]
        });
    });
</script>
</body>
</html>


using System;
using System.Web.Script.Serialization;

namespace csdemo.extjs
{
    public partial class jsonresponse : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int resMode;
            JavaScriptSerializer js = new JavaScriptSerializer();
            responseMsg resMsg = new responseMsg();
            blogEssay bl = new blogEssay();


            if (Request["resMode"] != null)
            {
                resMode = Convert.ToInt32(Request["resMode"]);
            }
            else
            {
                resMode = 2;
            }

            switch (resMode)
            {
                case 1:
                    resMsg.success = true;
                    resMsg.errorMsg = "表单接收成功!";
                    Response.Write(js.Serialize(resMsg));
                    break;
                case 2:
                    bl.success = true;
                    bl.title = "『ExtJS』表单(二)表单行为与Asp.NET页面的消息回复";
                    bl.author = "Aaron";
                    bl.contentEssay = "test";
                    bl.site = "http://www.cnblogs.com/sitemanager/";
                    Response.Write(js.Serialize(bl));
                    break;
                case 3:
                    resMsg.success = true;
                    resMsg.errorMsg = "Thank you for your reading!";
                    Response.Write(js.Serialize(resMsg));
                    break;
                case 4:
                    resMsg.success = true;
                    resMsg.errorMsg = "参数传入成功!";
                    Response.Write(js.Serialize(resMsg));
                    break;
                default:
                    resMsg.success = false;
                    resMsg.errorMsg = "请先传入参数!";
                    Response.Write(js.Serialize(resMsg));
                    break;
            }
        }
    }

    public class responseMsg
    {
        private bool _success;

        public bool success
        {
            get { return _success; }
            set { _success = value; }
        }
        private string _errorMsg;

        public string errorMsg
        {
            get { return _errorMsg; }
            set { _errorMsg = value; }
        }
    }

    public class blogEssay : responseMsg
    {
        private string _title;

        public string title
        {
            get { return _title; }
            set { _title = value; }
        }
        private string _author;

        public string author
        {
            get { return _author; }
            set { _author = value; }
        }
        private string _email;

        public string email
        {
            get { return _email; }
            set { _email = value; }
        }
        private string _site;

        public string site
        {
            get { return _site; }
            set { _site = value; }
        }
        private int _publishNumber;

        public int publishNumber
        {
            get { return _publishNumber; }
            set { _publishNumber = value; }
        }
        private string _abstractEssay;

        public string abstractEssay
        {
            get { return _abstractEssay; }
            set { _abstractEssay = value; }
        }
        private DateTime _publishDate;

        public DateTime publishDate
        {
            get { return _publishDate; }
            set { _publishDate = value; }
        }
        private string _contentEssay;

        public string contentEssay
        {
            get { return _contentEssay; }
            set { _contentEssay = value; }
        }
    }
}

 

说明:

  1. 在submit()函数中,可以使用 url 来重新指定表单回传的目标页面
  2. 可以使用 params 来指定回传的参数,默认为post方式
  3. 有后台Asp.NET中,直接使用 Request[‘参数名 ']来获取传入的值
  4. 注意,从前台来的值,一般情况下是string型的,可能会需要进行一些格式上的转换后,才能使用

 

 

效果图

image



资源下载链接为: https://pan.quark.cn/s/1bfadf00ae14 在 Linux 系统中,查找域名或主机名对应的 IP 地址是网络管理中的一项基础且关键任务,对于排查网络故障、调试网络问题以及监控网络服务是否正常运行等场景都非常重要。本文将介绍五种在 Linux 终端查询域名 IP 地址的方法。 首先,dig 命令(全称 Domain Information Groper)是一个功能强大的 DNS 查询工具,能够向 DNS 服务器发送查询请求并获取详细的响应信息。如果需要查询单个域名的 IP 地址,可以使用命令 dig 2daygeek.com +short 。此外,还可以通过编写 bash 脚本,将包含域名的文本文件中的域名逐个读取,然后利用 dig 命令进行查询,从而实现批量查询域名 IP 地址的功能。 其次,host 命令是一个简单易用的 DNS 查询工具,主要用于将域名解析为 IP 地址。要获取某个域名的 IP 地址,直接使用 host 2daygeek.com 即可。如果只想显示 IP 地址部分,可以通过管道结合 grep 和 sed 命令来实现,例如:host 2daygeek.com | grep "has address" | sed s/has address/-/g 。 再者,nslookup 命令也是一种常用的 DNS 查询工具,它支持交互式查询 DNS 信息。通过 nslookup 2daygeek.com 可以查询域名的 IP 地址。若要以非交互式的方式只显示 IP 地址,可以使用命令 nslookup 2daygeek.com | awk /^Address:/ {print $2} 。 另外,fping 命令传统的 ping 命令不同,它不会直接进行 DNS 查询,而是通过发送 ICMP Echo Request(pi
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值