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

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

几点说明


  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



传送带损坏对象检测数据集 一、基础信息 • 数据集名称:传送带损坏对象检测数据集 • 图片数量: 训练集:645张图片 验证集:185张图片 测试集:92张图片 总计:922张工业监控图片 • 训练集:645张图片 • 验证集:185张图片 • 测试集:92张图片 • 总计:922张工业监控图片 • 分类类别: Hole(孔洞):传送带表面的孔洞损坏。 Human(人类):工作区域中的人类,用于安全监控。 Other Objects(其他对象):非预期对象,可能引起故障。 Puncture(刺穿):传送带被刺穿的损坏。 Roller(滚筒):传送带滚筒部件。 Tear(撕裂):传送带撕裂损坏。 impact damage(冲击损坏):由于冲击导致的损坏。 patch work(修补工作):已修补的区域。 • Hole(孔洞):传送带表面的孔洞损坏。 • Human(人类):工作区域中的人类,用于安全监控。 • Other Objects(其他对象):非预期对象,可能引起故障。 • Puncture(刺穿):传送带被刺穿的损坏。 • Roller(滚筒):传送带滚筒部件。 • Tear(撕裂):传送带撕裂损坏。 • impact damage(冲击损坏):由于冲击导致的损坏。 • patch work(修补工作):已修补的区域。 • 标注格式:YOLO格式,包含边界框和类别标签,适用于目标检测任务。 • 数据格式:图像数据来源于工业监控系统,适用于计算机视觉分析。 、适用场景 • 工业自动化检测系统开发:用于构建自动检测传送带损坏和异物的AI模型,实现实时监控和预防性维护,减少停机时间。 • 安全监控应用:识别人类和其他对象,提升工业环境的安全性,避免事故和人员伤害。 • 学术研究创新:支持计算机视觉在制造业、物流和自动化领域的应用研究,促进AI技术工业实践的融合。 • 教育培训:可用于培训AI模型或作为工业工程和自动化教育的案例数据,帮助学习者理解实际应用场景。 三、数据集优势 • 多样化的类别覆盖:包含8个关键类别,涵盖多种损坏类型和对象,确保模型能够处理各种实际工业场景,提升泛化能力。 • 精准的标注质量:采用YOLO格式,边界框标注准确,由专业标注人员完成,保证数据可靠性和模型训练效果。 • 强大的任务适配性:兼容主流深度学习框架(如YOLO、TensorFlow、PyTorch),可直接用于目标检测任务,并支持扩展至其他视觉任务需求。 • 突出的工业价值:专注于工业传送带系统的实际需求,帮助提升生产效率、降低维护成本,并增强工作场所安全,具有较高的实际应用价值。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值