zoukankan      html  css  js  c++  java
  • ASP.NET偷懒大法四(动态生成表单对象)

    做表单提交时,经常的做法是拖TextBox等控件到页面,然后点提交按钮,cs代码把控件的值一个一个的赋给一个对象,然后调用一个Save的方法 提交成功!烦琐的操作,基本在每一个表单添加修改的时候做一遍。我们这些烦琐的事情交给.net做吧。

    从Panel继承一个自定义控件,当成一个区间,写一个方法遍历这个区间所有的控件,自动给类赋值

    首页给这个控件一个属性

    public string Entity
            {
                get
                {
                    String s = (String)ViewState["Entity"];
                    return ((s == null) ? String.Empty : s);
                }

                set
                {
                    ViewState["Entity"] = value;
                }
            }

    属性的值就是这个表单所操作的实体类

    然后写个方法,反射实体类,变量区间中所有的控件,赋值

    public object GetEntity()
            {
                Type t = Type.GetType(Entity, true);
                object entity = Activator.CreateInstance(t);
                foreach (object var in this.Controls)
                {
                    Type ControlType = var.GetType();
                    switch (ControlType.Name)
                    {
                        case "SMTextBox":
                            if (!string.IsNullOrEmpty((var as FrameworkWeb.Web.SMTextBox).ID))
                            {
                                System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as FrameworkWeb.Web.SMTextBox).ID);
                                if (propertyInfo != null)
                                {
                                    string tempText = (var as FrameworkWeb.Web.SMTextBox).Text;
                                    if (!string.IsNullOrEmpty(tempText))
                                        propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                                }
                            }
                            break;
                        case "TextBox":
                            if (!string.IsNullOrEmpty((var as TextBox).ID))
                            {
                                System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as TextBox).ID);
                                if (propertyInfo != null)
                                {
                                    string tempText = (var as TextBox).Text;
                                    if (!string.IsNullOrEmpty(tempText))
                                        propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                                }
                            }
                            break;
                        case "SMDTPicker":
                            if (!string.IsNullOrEmpty((var as SMDTPicker).ID))
                            {
                                System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as SMDTPicker).ID);
                                if (propertyInfo != null)
                                {
                                    string tempText = (var as SMDTPicker).Text;
                                    if (!string.IsNullOrEmpty(tempText))
                                        propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                                }
                            }
                            break;
                        case "DropDownList":
                            if (!string.IsNullOrEmpty((var as DropDownList).ID))
                            {
                                System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as DropDownList).ID);
                                if (propertyInfo != null)
                                {
                                    string tempText = (var as DropDownList).SelectedValue;
                                    propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                                }
                            }
                            break;

                        case "HiddenField":
                            if (!string.IsNullOrEmpty((var as HiddenField).ID))
                            {
                                System.Reflection.PropertyInfo propertyInfo = t.GetProperty((var as HiddenField).ID);
                                if (propertyInfo != null)
                                {
                                    string tempText = (var as HiddenField).Value;
                                    propertyInfo.SetValue(entity, Convert.ChangeType(tempText, propertyInfo.PropertyType), null);
                                }
                            }
                            break;
                    }
                }

                return entity;
            }

    前台调用的时候直接执行这个方法就可以得到这个对象了,如果有一些特殊的情况,再二次处理就ok啦。

    然后就是修改啦,修改的时候需要先给控件赋值,然后用户修改后提交。这个也可以用类似的原来简化操作。

    给我们的控件增加一个数据库源的属性

    public virtual object DataSource
            {
                get
                {
                    return this.dataSource;
                }

                set
                {
                    this.dataSource = value;
                }
            }

    增加一个数据绑定的时候的事件

    public delegate void SMFormDataBindEventHandler(object source, SMFormEventArgs e);

    [Category("Action"), Description("DataBind")]
    public event SMFormDataBindEventHandler DataBindCommand;

    ///这里存的是自动赋好值的一个对象,方便二次处理
    public class SMFormEventArgs : EventArgs
        {
            public readonly object tempObject;
            public SMFormEventArgs(object tempObject)
            {
                this.tempObject = tempObject;
            }
        }

    这是为了处理特殊的情况,有时候有些控件可能不是直接取数据库的值,就可以利用这个事件进行二次处理啦

    然后给控件赋值

    protected new void DataBind()
            {
                PropertyInfo p;

                foreach (Control var in this.Controls)
                {
                    switch (var.GetType().Name.ToUpper())
                    {
                        case "TEXTBOX":
                            System.Web.UI.WebControls.TextBox textBox = var as System.Web.UI.WebControls.TextBox;
                            p = DataSource.GetType().GetProperty(var.ID);
                            if (p != null)
                                textBox.Text = p.GetValue(DataSource, null).ToString().Trim();
                            break;
                        case "SMTEXTBOX":
                            FrameworkWeb.Web.SMTextBox smtextBox = var as FrameworkWeb.Web.SMTextBox;
                            p = DataSource.GetType().GetProperty(var.ID);
                            if (p != null)
                                smtextBox.Text = p.GetValue(DataSource, null).ToString().Trim();
                            break;
                        case "DROPDOWNLIST":
                            System.Web.UI.WebControls.DropDownList ddl = var as System.Web.UI.WebControls.DropDownList;
                            p = DataSource.GetType().GetProperty(var.ID);
                            if (p != null)
                            {
                                object o = p.GetValue(DataSource, null);
                                if (o!=null)
                                ddl.SelectedValue = p.GetValue(DataSource, null).ToString().Trim();
                            }
                            break;
                        case "LITERAL":
                            System.Web.UI.WebControls.Literal lit = var as System.Web.UI.WebControls.Literal;
                            p = DataSource.GetType().GetProperty(lit.ID);
                            if (p != null)
                                lit.Text = p.GetValue(DataSource, null).ToString().Trim();
                            break;
                        case "HIDDENFIELD":
                            System.Web.UI.WebControls.HiddenField hf = var as System.Web.UI.WebControls.HiddenField;
                            p = DataSource.GetType().GetProperty(hf.ID);
                            if (p != null)
                                hf.Value = p.GetValue(DataSource, null).ToString().Trim();
                            break;
                        case "SMDTPICKER":
                            FrameworkWeb.Web.SMDTPicker smDTPicker = var as FrameworkWeb.Web.SMDTPicker;
                            p = DataSource.GetType().GetProperty(var.ID);
                            if (p != null)
                                smDTPicker.Text = p.GetValue(DataSource, null).ToString().Trim();
                            break;
                            break;
                    }
                }
            }

    这样这个控件就基本完成了,调用的时候基本不用写什么代码啦

    拖好控件,在提交的事件中只需要写

    Model.BTS item = smform.GetEntity() as Model.BTS;
    DAL.BTS.Save(item);

    修改的时候

    Model.BTS btsModel== DAL.BTS.GetModel("sCode");
    smform.DataSource = btsModel;

    然后在事件中做二次处理

    protected void smform_DataBindCommand(object source, FrameworkWeb.Form.SMFormEventArgs e)
        {
            Model.BTS item = e.tempObject as Model.BTS;
            if (item != null)
            {
                DropDownList dp = smform.FindControl("isEnable") as DropDownList;
                dp.Items.FindByValue(item.isEnable.ToString()).Selected = true;
                if (item.dicSectorType>0)
                    (smform.FindControl("dicSectorType") as DropDownList).Items.FindByValue(item.dicSectorType.ToString()).Selected = true;
                if (item.dicAreaType > 0)
                (smform.FindControl("dicAreaType") as DropDownList).Items.FindByValue(item.dicAreaType.ToString()).Selected = true;

                //if (item.fkDeviceID > 0)
                //    (smform.FindControl("fkDeviceID") as DropDownList).Items.FindByValue(item.fkDeviceID.ToString()).Selected = true;
            }
        }

    然后保存
    Model.BTS item = smform.GetEntity() as Model.BTS;
    DAL.BTS.Update(item);

    一切就是如此easy :)

  • 相关阅读:
    Atitit 网络技术体系图 目录 1. 的三网融合是 1 1.1. 电话网、有线电视网 1 1.2. 计算机网 1 2. 计算机网 1 2.1. 互联网 1 2.2. 局域网 1 3. 第1章 计
    Atitit 非结构化数据管理法 目录 1. 什么是非结构化数据? 1 2. 对非结构化数据也即对全文数据的搜索主要有两种方法: 2 2.1. 顺序扫描法(Serial Scanning): 2 2
    Atitit 可视化技术体系题 目录 1. 1. 可视化分类 1 1 1.1. 1.1. 层次可视化 金字塔等 层次降为3层归类可视化 1 1 1.2. 1.2. 高层可视化 鸟瞰可视化 1 1 1
    Atitit 持久化与数据存储标准化规范 目录 1. 存储的附加功能 2 1.1. 基本存取功能 2 1.2. 全文检索(imap 2 1.3. 属性检索 2 1.4. 查询语言 2 2. 基于内容
    Atitit 数据库技术体系 艾提拉总结 目录 1. 2. 初始概念 5 2 1.1. 2.1. 数据库的类型,网状,层次,树形数据库,kv数据库。Oodb 多媒体数据库 5 2 1.2. 2.2.
    Atitit 大数据体系图 大数据 技术 数据采集 gui自动化 爬虫 Nui自动化  Ocr技术 Tts语音处理 文档处理(office zip等) html文档处理解析 转
    Atitit 数据挖掘技术体系 目录 1. 统计分析(分组聚合等 1 2. Tag标注 结构化 1 2.1. · 复杂数据类型挖掘(Text, Web 2 2.2. ,图形图像,视频,音频等) 2
    Atitit  信息管理 艾提拉著 CAPT2 数据存储与分类 聚集.docx 目录 1. 按照存储位置 1 1.1. 网盘 1 1.2. 存储在eml imap中 方便检索 1 1.3. 分散与
    Atitit  信息管理 艾提拉著作 CAPT1信息源数据源 目录 1. 数据元的数据格式 图片 文本 视频 音频 2 2. 按照应用功能使用分类 2 2.1. Diary Cyarlog 2
    Atitit 计算软件简史 艾提拉著 目录 1.1. 第二代软件(1959~1965) 高级语言 第三代软件(1965~1971) os 1 1.2. 第四代软件(1971~1989)结构化的程序
  • 原文地址:https://www.cnblogs.com/SoulStore/p/1222917.html
Copyright © 2011-2022 走看看