webform快速创建表单内容文件--oracle 数据库

使用方法

前台页面这样写就足够了

<form class="stdform" runat="server">
     <div id="field_tab_content" runat="server"></div>
</form>

新增编辑加载页面(改页面需要继承CreateModel类)

Type type;
public decimal id = 0;
protected void Page_Init(object sernder, EventArgs e)
{
       //初始化类型
       this.XTYPE = new S_NARTICLE();
       S_NARTICLE pros = new S_NARTICLE();
       dec
       if (!string.IsNullOrEmpty(Request["id"]))
        {
              id = Convert.ToDecimal(Request["id"]);
              if(id>0)
              {
                      //通过id获取项目信息
                     XTYPE = data.S_NARTICLE.FirstOrDefault(p => p.ARTID == id);
              }
        }
      //设置特殊字段
      fileds.Add(new FiledInfo() { FiledName = "PID", IsHide = true });
      //设置下拉列表数据
      //设置下拉类表选项
      List<DictionaryEntry> industriesTypeList = new List<DictionaryEntry>();
      industriesTypeList.Add(new DictionaryEntry("是", "T"));
      industriesTypeList.Add(new DictionaryEntry("否", "F"));    
      SpecialControls.Add(new EditPageControl() { CType = ContorlType.select, FiledName ="字段名称", Value = industriesTypeList});
      this.CreateData1(type);
      field_tab_content.Controls.Add(Cache[type.Name + "htmlDD"] as Control);  
}
View Code

保存页面

protected void btnSave_Click(object sender, EventArgs e)
{
     this.Save1(field_tab_content, type, XTYPE);
     S_NARTICLE project = XTYPE as S_NARTICLE;
    if (project.ARTID == 0)
    {
        //新增操作
        //特殊字段处理。。。。
    }else{
        //修改操作
        //特殊字段处理。。。。
    }
}
View Code

CreateModel类

public class CreateModel : System.Web.UI.Page
{

       public object XTYPE; 
        public List<FiledInfo> fileds = new List<FiledInfo>();//设置属性
        public List<EditPageControl> SpecialControls = new List<EditPageControl>();//设置属性
        public List<L_CATTACHMENT> filelist = new List<L_CATTACHMENT>();//附件信息 
        public StringBuilder text = new StringBuilder();
        public bool isReadonly = false;
        public bool isAllalien = false;
//创建表单内容文件
public void NewList(Type type)
        {
            HtmlGenericControl htmlDD = new HtmlGenericControl("div");
            htmlDD.Attributes.Add("class", "boxcontents procontent");
            if (Cache["" + type.Name + ""] == null)
            {
                //获取数据oracle数据库中的字段名称和备注
                DataSet ds = DbHelperOleDb.GetListBySql("select table_name,column_Name,Comments from user_col_comments where table_name='" + type.Name + "'");
                Cache["" + type.Name + ""] = ds;
            }
            if (Cache["" + type.Name + ""] != null)
            {
                foreach (DataRow item in (Cache["" + type.Name + ""] as DataSet).Tables[0].Rows)
                {
                    //没有预置的字段
                    if (fileds.Count == 0 || fileds.Count(p => p.FiledName.ToUpper() == item["column_Name"].ToString()) == 0)
                    {
                        fileds.Add(new FiledInfo() { FiledName = item["column_Name"].ToString(), FiledDesc = item["Comments"].ToString(), Index = 0 });
                    }
                    else
                    {
                        var f = fileds.FirstOrDefault(p => p.FiledName.ToUpper() == item["column_Name"].ToString());
                        if (f != null)
                            f.FiledDesc = item["Comments"].ToString();
                    }

                }
                fileds = fileds.OrderBy(p => p.Index).ToList();

                foreach (var filed in fileds)
                {
                    //隐藏字段不处理
                    if (filed.IsHide)
                        continue;
                    //设置占全行
                    if (isAllalien)
                    {
                        filed.AllLine = isAllalien;
                    }
                    #region  boxdiv设置表单每项的外部样式
                    HtmlGenericControl boxdiv = new HtmlGenericControl("div");//boxdiv
                    var boxdivclass = ConfigurationManager.AppSettings["boxdiv"];
                    if (!string.IsNullOrEmpty(boxdivclass))
                    {

                        boxdiv.Attributes.Add("class", boxdivclass);
                    }
                    else
                    {
                        boxdiv.Attributes.Add("class", "listitem form-group");
                    }
                    #region boxChildlabel
                    HtmlGenericControl boxChildlabel = new HtmlGenericControl("label");//boxChildlabel
                    var boxChildlabelclass = ConfigurationManager.AppSettings["boxChildlabel"];
                    if (!string.IsNullOrEmpty(boxChildlabelclass))
                    {

                        boxChildlabel.Attributes.Add("class", boxChildlabelclass);
                    }
                    else
                    {
                        boxChildlabel.Attributes.Add("class", "listleft titles");
                    }
                    boxChildlabel.InnerText = filed.FiledDesc + ":";
                    #endregion

                    HtmlGenericControl boxChilddiv = new HtmlGenericControl("div");//boxChilddiv
                    var boxChilddivclass = ConfigurationManager.AppSettings["boxChilddiv"];
                    if (!string.IsNullOrEmpty(boxChilddivclass))
                    {

                        boxChilddiv.Attributes.Add("class", boxChilddivclass);
                    }
                    else
                    {
                        boxChilddiv.Attributes.Add("class", "listleft con");
                    }
                    PropertyInfo cons = type.GetProperty(filed.FiledName);
                    #endregion

                    var specialContorl = SpecialControls.FirstOrDefault(p => p.FiledName.ToLower() == filed.FiledName.ToLower());
                    if (specialContorl == null)
                    {
                        specialContorl = SpecialControls.FirstOrDefault(p => p.FiledName.ToUpper() == filed.FiledName.ToUpper());

                    }
                    if (specialContorl != null)
                    {
                        #region 处理特殊控件
                        switch (specialContorl.CType)
                        {
                            case ContorlType.checkbox:
                                CheckBoxList dts = new CheckBoxList();
                                dts.ID = "S_" + filed.FiledName;
                                dts.RepeatDirection = RepeatDirection.Horizontal;
                                dts.RepeatColumns = 4;
                                filed.AllLine = true;
                                if (specialContorl.Value != null)
                                {
                                    foreach (var item in specialContorl.Value as List<DictionaryEntry>)
                                    {
                                        dts.Items.Add(new ListItem() { Text = item.Key + "", Value = item.Value + "" });
                                    }
                                }
                                dts.Enabled = !isReadonly;
                                //只读显示
                                HtmlGenericControl spandts = new HtmlGenericControl("span");
                                spandts.InnerText = "{{project." + filed.FiledName.ToLower() + "}}";
                                spandts.Attributes.Add("v-show", "isshow");

                                #region 多选按钮样式
                                var checkboxclass = ConfigurationManager.AppSettings[ContorlType.checkbox.ToString()];
                                if (!string.IsNullOrEmpty(checkboxclass))
                                {

                                    dts.Attributes.Add("class", checkboxclass);
                                }
                                else
                                {
                                    dts.Attributes.Add("class", "");
                                }
                                if (!string.IsNullOrEmpty(specialContorl.thisclass))
                                {

                                    dts.Attributes.Add("class", specialContorl.thisclass);
                                }
                                #endregion
                                if (isReadonly)
                                {
                                    boxChilddiv.Controls.Add(spandts);
                                }
                                else
                                {
                                    boxChilddiv.Controls.Add(dts);
                                }
                                break;
                            case ContorlType.select:
                                DropDownList ddl = new DropDownList();
                                ddl.ID = "S_" + filed.FiledName;
                                ddl.Enabled = !isReadonly;
                                if (filed.Readonly)
                                {
                                    ddl.Enabled = false;
                                }
                                string selectname = "";
                                if (specialContorl.Value != null)
                                {

                                    var items = specialContorl.Value as List<DictionaryEntry>;
                                    ddl.DataSource = items;
                                    ddl.DataValueField = "value";
                                    ddl.DataTextField = "key";
                                    ddl.DataBind();
                                    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(cons.GetValue(XTYPE) + ""));
                                    if (filed.FiledDesc == "采购人角色" || filed.FiledDesc == "采购代理机构角色")
                                    {
                                        ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue("04"));
                                    }
                                    var selvalu = cons.GetValue(XTYPE);
                                    var po = items.FirstOrDefault(x => x.Value == selvalu);
                                    selectname = ddl.SelectedItem.Text;
                                }
                                HtmlGenericControl spandtsd = new HtmlGenericControl("span");
                                //spandtsd.InnerText = "{{project." + filed.FiledName.ToLower() + "}}" + selectname;
                                spandtsd.InnerText = selectname;
                                spandtsd.Attributes.Add("v-show", "isshow");
                                ddl.Attributes.Add("style", "width:98%;");

                                #region 下拉列表样式设置
                                var selectclass = ConfigurationManager.AppSettings[ContorlType.select.ToString()];
                                if (!string.IsNullOrEmpty(selectclass))
                                {

                                    ddl.Attributes.Add("class", selectclass);
                                }
                                else
                                {
                                    ddl.Attributes.Add("class", "");
                                }
                                if (!string.IsNullOrEmpty(specialContorl.thisclass))
                                {

                                    ddl.Attributes.Add("class", specialContorl.thisclass);
                                }
                                #endregion
                                if (isReadonly)
                                {
                                    boxChilddiv.Controls.Add(spandtsd);
                                }
                                else
                                {
                                    boxChilddiv.Controls.Add(ddl);
                                }
                                break;
                            case ContorlType.FontColor:
                                HtmlTextArea txtTextArea1s = new HtmlTextArea();
                                txtTextArea1s.ID = "S_" + filed.FiledName;
                                txtTextArea1s.InnerText = cons.GetValue(XTYPE) + "";

                                #region 字体样式设置
                                var FontColorclass = ConfigurationManager.AppSettings[ContorlType.FontColor.ToString()];
                                if (!string.IsNullOrEmpty(FontColorclass))
                                {

                                    txtTextArea1s.Attributes.Add("class", FontColorclass);
                                }
                                else
                                {
                                    txtTextArea1s.Attributes.Add("class", "");
                                }
                                if (!string.IsNullOrEmpty(specialContorl.thisclass))
                                {

                                    txtTextArea1s.Attributes.Add("class", specialContorl.thisclass);
                                }
                                #endregion

                                if (isReadonly)
                                {
                                    txtTextArea1s.Attributes.Add("readOnly", isReadonly + "");
                                }
                                HtmlGenericControl sp = new HtmlGenericControl("span");
                                sp.InnerText = "{{project." + filed.FiledName.ToLower() + "}}";
                                sp.Attributes.Add("v-show", "isshow");
                                if (isReadonly)
                                {
                                    boxChilddiv.Controls.Add(sp);
                                }
                                else
                                    boxChilddiv.Controls.Add(txtTextArea1s);

                                break;
                            case ContorlType.uedit:

                                HtmlTextArea txtTextArea1 = new HtmlTextArea();
                                txtTextArea1.ID = "S_" + filed.FiledName;
                                txtTextArea1.Attributes.Add("class", "show");
                                txtTextArea1.InnerText = cons.GetValue(XTYPE) + "";
                                filed.AllLine = true;

                                #region 编辑器设置
                                var ueditclass = ConfigurationManager.AppSettings[ContorlType.uedit.ToString()];
                                if (!string.IsNullOrEmpty(ueditclass))
                                {

                                    txtTextArea1.Attributes.Add("class", ueditclass);
                                }
                                else
                                {
                                    txtTextArea1.Attributes.Add("class", "");
                                }
                                if (!string.IsNullOrEmpty(specialContorl.thisclass))
                                {

                                    txtTextArea1.Attributes.Add("class", specialContorl.thisclass);
                                }
                                if (isReadonly)
                                {
                                    txtTextArea1.Attributes.Add("readOnly", isReadonly + "");
                                }
                                HtmlGenericControl ssp = new HtmlGenericControl("span");
                                ssp.InnerText = "{{project." + filed.FiledName.ToLower() + "}}";
                                ssp.Attributes.Add("v-show", "isshow");
                                if (isReadonly)
                                {
                                    boxChilddiv.Controls.Add(ssp);
                                }
                                else
                                    boxChilddiv.Controls.Add(txtTextArea1);
                                #endregion
                                break;
                            case ContorlType.fileUpload:

                                TextBox box = new TextBox();
                                box.ID = "S_" + filed.FiledName;
                                box.Text = cons.GetValue(XTYPE) + "";
                                if (!isReadonly)
                                {
                                    box.Attributes.Add("class", "input code input normal upload-path");
                                }

                                box.Attributes.Add("style", "width:82%");
                                HtmlGenericControl imgdiv = new HtmlGenericControl("div");


                                HtmlGenericControl ssps = new HtmlGenericControl("span");
                                ssps.InnerText = "{{project." + filed.FiledName.ToLower() + "}}";
                                ssps.Attributes.Add("v-show", "isshow");

                                #region 文件上传样式设置
                                var fileUploadclass = ConfigurationManager.AppSettings[ContorlType.fileUpload.ToString()];
                                if (!string.IsNullOrEmpty(fileUploadclass))
                                {

                                    box.Attributes.Add("class", fileUploadclass);
                                }
                                else
                                {
                                    box.Attributes.Add("class", "upload-box upload-img");
                                }
                                if (!string.IsNullOrEmpty(specialContorl.thisclass))
                                {

                                    box.Attributes.Add("class", specialContorl.thisclass);
                                }
                                if (!isReadonly)
                                {
                                    imgdiv.Attributes.Add("class", "upload-box upload-img");
                                }
                                box.ReadOnly = isReadonly;
                                if (isReadonly)
                                {
                                    boxChilddiv.Controls.Add(ssps);
                                }
                                else
                                {
                                    boxChilddiv.Controls.Add(box);
                                }
                                #endregion
                                break;
                            case ContorlType.fileUploadmore:

                                HtmlGenericControl boxd = new HtmlGenericControl();
                                boxd.ID = "S_" + filed.FiledName;
                                var filecontent = cons.GetValue(XTYPE) + "";

                                HtmlGenericControl imgdivs = new HtmlGenericControl("div");
                                imgdivs.Attributes.Add("class", "photo-list");
                                HtmlGenericControl ul = new HtmlGenericControl("ul");
                                #region 添加相应的内容
                                //获取相应的内容  并将值赋值到相应的控件上
                                if (!string.IsNullOrEmpty(filecontent))
                                {
                                    var name = filecontent;
                                    //基本信息
                                    if (!string.IsNullOrEmpty(name))
                                    {
                                        List<decimal?> Pids = new List<decimal?>();
                                        string[] AllPid = name.Trim(',').Split(',');
                                        for (int i = 0; i < AllPid.Length; i++)
                                        {

                                            HtmlGenericControl li = new HtmlGenericControl("li");
                                            var da = Convert.ToDecimal(AllPid[i]);
                                            var filemodel = filelist.FirstOrDefault(x => x.ATTACHMENTID == da);
                                            if (filemodel != null)
                                            {
                                                HtmlGenericControl hid_photo_name = new HtmlGenericControl("input");
                                                hid_photo_name.Attributes.Add("name", "hid_photo_name" + filed.FiledName);
                                                hid_photo_name.Attributes.Add("type", "hidden");
                                                hid_photo_name.Attributes.Add("value", "" + filemodel.ATTACHMENTID + "|" + filemodel.FILEOLDNAME + "|" + filemodel.FILESIZE + "|" + filemodel.FILEPATH + "|" + filemodel.FILEPATH + "");
                                                HtmlGenericControl hid_photo_remark = new HtmlGenericControl("input");
                                                hid_photo_remark.Attributes.Add("name", "hid_photo_remark" + filed.FiledName);
                                                hid_photo_remark.Attributes.Add("type", "hidden");
                                                hid_photo_remark.Attributes.Add("value", filemodel.FILEOLDNAME);

                                                HtmlGenericControl contentdiv = new HtmlGenericControl("div");
                                                contentdiv.Attributes.Add("class", "img-box");
                                                contentdiv.Attributes.Add("style", "height:25px;");
                                                //contentdiv.Attributes.Add("onclick", "setFocusImg(this);");


                                                //HtmlGenericControl contentspan = new HtmlGenericControl("span");
                                                HtmlGenericControl contentspan = new HtmlGenericControl("a");
                                                contentspan.Attributes.Add("class", "remark");
                                                contentspan.Attributes.Add("href", filemodel.FILEPATH);
                                                //<a target="_blank"></a>
                                                contentspan.Attributes.Add("target", "_blank");

                                                HtmlGenericControl contenti = new HtmlGenericControl("i");
                                                contenti.InnerText = filemodel.FILEREMARK == "" ? filemodel.FILEOLDNAME : filemodel.FILEREMARK;
                                                contentspan.Controls.Add(contenti);
                                                contentdiv.Controls.Add(contentspan);

                                                //HtmlGenericControl remarda = new HtmlGenericControl("a");
                                                //remarda.InnerText = "描述";
                                                //remarda.Attributes.Add("href", "javascript:;");
                                                //remarda.Attributes.Add("onclick", "setRemark(this);");
                                                //HtmlGenericControl del = new HtmlGenericControl("a");
                                                //del.InnerText = "删除";
                                                //del.Attributes.Add("href", "javascript:;");
                                                //del.Attributes.Add("onclick", "delImg(this);");
                                                li.Controls.Add(hid_photo_name);//基本信息
                                                li.Controls.Add(hid_photo_remark);//备注 
                                                li.Controls.Add(contentdiv);//备注
                                                //li.Controls.Add(remarda);//备注
                                                //li.Controls.Add(del);//删除
                                                ul.Controls.Add(li);
                                            }
                                        }
                                    }

                                }
                                #endregion

                                imgdivs.Controls.Add(ul);
                                boxChilddiv.Controls.Add(boxd);
                                HtmlGenericControl sspsd = new HtmlGenericControl("span");
                                sspsd.InnerText = "{{project." + filed.FiledName.ToLower() + "}}";
                                sspsd.Attributes.Add("v-show", "isshow");
                                //if (isReadonly)
                                //{
                                //    boxChilddiv.Controls.Add(sspsd);
                                //}
                                //else
                                #region 文件上传样式设置
                                var fileUploadmoreclass = ConfigurationManager.AppSettings[ContorlType.fileUploadmore.ToString()];
                                if (!string.IsNullOrEmpty(fileUploadmoreclass))
                                {
                                    boxd.Attributes.Add("class", fileUploadmoreclass);
                                }
                                if (!string.IsNullOrEmpty(specialContorl.thisclass))
                                {

                                    boxd.Attributes.Add("class", specialContorl.thisclass);
                                }
                                if (!isReadonly)
                                {
                                    boxd.Attributes.Add("class", "upload-box upload-album");
                                    boxd.Attributes.Add("value", filecontent + "");
                                }
                                #endregion
                                boxChilddiv.Controls.Add(imgdivs);
                                break;
                            default:
                                break;
                        }
                        #endregion
                    }
                    else
                    {
                        #region 处理一般控件
                        PropertyInfo con = type.GetProperty(filed.FiledName);
                        if (con != null)
                        {
                            var valtype = con.PropertyType;
                            if (valtype.IsGenericType && valtype.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                            {
                                //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换  
                                System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(valtype);
                                //将type转换为nullable对的基础基元类型  
                                valtype = nullableConverter.UnderlyingType;
                            }
                            TextBox txtControl = new TextBox();
                            if (valtype.Name == "Decimal" || valtype.Name == "Single")
                            {
                                filed.IsRequired = true;
                                filed.VerificationStrType = VerificationStr.decmilnumber;
                                //txtControl.Attributes.Add("type", "number");
                                txtControl.CssClass = "mediuminput";
                                txtControl.Text = "0";//设置初始值
                            }
                            else if (valtype.Name == "DateTime")
                            {
                                filed.VerificationStrType = VerificationStr.checkFullTime;
                                txtControl.CssClass = "laydate-icon";
                                if (!isReadonly)
                                {
                                    if (filed.FiledName == "PUBTIME")
                                    {
                                        txtControl.Attributes.Add("onclick", "laydate({istime: true, format: 'YYYY-MM-DD'})");
                                    }
                                    else
                                    {
                                        txtControl.Attributes.Add("onclick", "laydate({istime: true, format: 'YYYY-MM-DD hh:mm:ss'})");
                                    }
                                }
                                //txtControl.Attributes.Add("style", "width:91%;float:left");

                            }
                            else
                            {
                                txtControl.CssClass = "mediuminput";
                            }

                            txtControl.ReadOnly = isReadonly;

                            //设置id
                            txtControl.ID = "S_" + filed.FiledName.ToString();
                            //赋值
                            if (XTYPE != null)
                            {
                                txtControl.Text = cons.GetValue(XTYPE) + "";
                                if (filed.FiledName == "STOPTIME")
                                {
                                    txtControl.Attributes.Add("placeholder", "不填写永久显示");
                                    txtControl.Text = cons.GetValue(XTYPE) + "" == "" ? "9999/12/31" : cons.GetValue(XTYPE) + "";
                                }
                            }

                            if (filed.Readonly)
                            {
                                txtControl.ReadOnly = true;
                            }
                            if (filed.FiledName == "PASSWORD")
                            {
                                txtControl.TextMode = TextBoxMode.Password;//设置密码框
                            }
                            txtControl.Attributes.Add("tipmsg", filed.FiledDesc);//设置属性
                            if (filed.FiledName == "PASSWORD")
                            {
                                txtControl.TextMode = TextBoxMode.Password;//设置密码框 

                            }
                            HtmlGenericControl sspsdd = new HtmlGenericControl("span");
                            sspsdd.InnerText = "{{project." + filed.FiledName.ToLower() + "}}";
                            sspsdd.Attributes.Add("v-show", "isshow");
                            txtControl.Attributes.Add("v-model", "project." + filed.FiledName.ToLower() + "");

                            #region 设置文本框样式
                            var fileUploadmoreclass = ConfigurationManager.AppSettings["input"];
                            if (!string.IsNullOrEmpty(fileUploadmoreclass))
                            {
                                txtControl.CssClass = fileUploadmoreclass;
                            }
                            if (!string.IsNullOrEmpty(filed.thisclass))
                            {

                                txtControl.CssClass = filed.thisclass;
                            }
                            //设置文本框类型
                            switch (filed.inputtpe)
                            {
                                case Inputtype.text:
                                case Inputtype.password:
                                case Inputtype.datetime:
                                case Inputtype.date:
                                case Inputtype.month:
                                case Inputtype.time:
                                case Inputtype.week:
                                case Inputtype.number:
                                case Inputtype.email:
                                case Inputtype.url:
                                case Inputtype.search:
                                case Inputtype.tel:
                                case Inputtype.color:
                                    txtControl.Attributes.Add("type", filed.inputtpe.ToString());
                                    break;
                                default:
                                    break;
                            }
                            #endregion

                            if (isReadonly)
                            {
                                boxChilddiv.Controls.Add(sspsdd);
                            }
                            else
                                boxChilddiv.Controls.Add(txtControl);
                        }
                        #endregion
                    }
                    if (filed.IsRequired)
                    {
                        HtmlGenericControl error = new HtmlGenericControl("span");
                        error.Attributes.Add("title", filed.errormessage);//添加提示信息 
                        error.Attributes.Add("type", filed.VerificationStrType + "");//添加提示信息
                        error.InnerText = "*";
                        error.Attributes.Add("style", "color:red;position: absolute;left: 0;folat:left");
                        boxChilddiv.Controls.Add(error);
                    }
                    boxdiv.Controls.Add(boxChildlabel);
                    boxdiv.Controls.Add(boxChilddiv);
                    htmlDD.Controls.Add(boxdiv);
                }
            }
            Cache[type.Name + "htmlDD"] = htmlDD;
        }
//获取值
public void Save1(HtmlGenericControl field_tab_content, Type type, object XTYPE)
        {
            #region 赋值
            foreach (var filed in fileds)
            {
                //取到属性
                PropertyInfo property = type.GetProperty(filed.FiledName.ToUpper());
                if (property != null)
                {
                    var valtype = property.PropertyType;
                    if (valtype.IsGenericType && valtype.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))//判断convertsionType是否为nullable泛型类  
                    {
                        //如果type为nullable类,声明一个NullableConverter类,该类提供从Nullable类到基础基元类型的转换  
                        System.ComponentModel.NullableConverter nullableConverter = new System.ComponentModel.NullableConverter(valtype);
                        //将type转换为nullable对的基础基元类型  
                        valtype = nullableConverter.UnderlyingType;
                    }
                    var specialContorl = SpecialControls.FirstOrDefault(p => p.FiledName.ToLower() == filed.FiledName.ToLower());
                    if (specialContorl != null)
                    {

                        #region 特殊控件取值
                        switch (specialContorl.CType)
                        {
                            case ContorlType.select:
                                DropDownList ddl = field_tab_content.FindControl("S_" + filed.FiledName) as DropDownList;
                                if (ddl != null)
                                {
                                    var ddlVal = ddl.SelectedItem.Value.Trim();
                                    if (ddlVal != "")
                                        property.SetValue(XTYPE, Convert.ChangeType(ddlVal, valtype), null);
                                }
                                break;
                            case ContorlType.fileUpload:
                                TextBox text = field_tab_content.FindControl("S_" + filed.FiledName) as TextBox; 
                                string fileNames = text.Text.Trim();
                                HiddenField hidFileNames = field_tab_content.FindControl("S_HID_" + filed.FiledName) as HiddenField;
                                if (hidFileNames != null)
                                    property.SetValue(XTYPE, Convert.ChangeType(hidFileNames.Value + fileNames.Trim(';'), valtype), null);
                                else
                                    property.SetValue(XTYPE, Convert.ChangeType(fileNames.Trim(';'), valtype), null);

                                break;
                            case ContorlType.fileUploadmore:

                                break;
                            case ContorlType.uedit:
                            case ContorlType.FontColor:
                                HtmlTextArea txtControls = field_tab_content.FindControl("S_" + filed.FiledName) as HtmlTextArea;
                                var ueditVal = txtControls.InnerText;
                                property.SetValue(XTYPE, Convert.ChangeType(ueditVal, valtype), null);
                                break;
                        }

                        #endregion
                    }
                    else
                    {

                        //一般控件取值
                        TextBox txtControl = field_tab_content.FindControl("S_" + filed.FiledName) as TextBox;
                        if (txtControl != null)
                        {
                            var val = txtControl.Text.Trim();//txtControl.Text.Trim();
                            decimal dl = 0;
                            DateTime dt;
                            if (valtype.Name == "Decimal" || valtype.Name == "Single")
                            {
                                if (decimal.TryParse(val, out dl))
                                {
                                    val = dl.ToString();
                                    property.SetValue(XTYPE, Convert.ChangeType(val, valtype), null);
                                }
                            }
                            else if (valtype.Name == "DateTime")
                            {
                                if (DateTime.TryParse(val, out dt))
                                {
                                    val = dt.ToString();
                                    property.SetValue(XTYPE, Convert.ChangeType(val, valtype), null);
                                }

                            }
                            else
                            {
                                property.SetValue(XTYPE, Convert.ChangeType(val, valtype), null);
                            }

                        }

                    }

                }

            }
            #endregion


        }
}
View Code

 需要用到的类和枚举

FiledInfo类

public class FiledInfo
    {
        /// <summary>
        /// 字段名
        /// </summary>
        public string FiledName { get; set; }
        /// <summary>
        /// 字段描述
        /// </summary>
        public string FiledDesc { get; set; }
        /// <summary>
        /// 在界面中的排序
        /// </summary>
        public double Index { get; set; }

        /// <summary>
        /// 是否只读
        /// </summary>
        [DefaultValue(false)]
        public bool Readonly { get; set; }
        /// <summary>
        /// 是否隐藏
        /// </summary>
        [DefaultValue(false)]
        public bool IsHide { get; set; }
        /// <summary>
        /// 是否占全行
        /// </summary>
        [DefaultValue(false)]
        public bool AllLine { get; set; }
        /// <summary>
        /// 是否为空
        /// </summary>
        public bool IsRequired { get; set; }
        /// <summary>
        /// 验证类型
        /// </summary>
        public VerificationStr VerificationStrType { get; set; }
        /// <summary>
        /// 提示信息
        /// </summary>
        public string errormessage { get; set; }
        /// <summary>
        /// 显示位置
        /// </summary>
        public ItemAlign itemalign { get; set; }
        /// <summary>
        /// 设置样式
        /// </summary>
        public string thisclass { get; set; }
        /// <summary>
        /// 文本框类型
        /// </summary>
        public Inputtype inputtpe { get; set; }

    }
FiledInfo

EditPageControl类

public class EditPageControl
    {/// <summary>
        /// 字段名
        /// </summary>
        public string FiledName { get; set; }

        /// <summary>
        /// 控件类型
        /// </summary>
        public ContorlType CType { get; set; }

        /// <summary>
        /// 取值
        /// </summary>
        public object Value { get; set; }
        /// <summary>
        /// 设置样式
        /// </summary>
        public string thisclass { get; set; }
    }
EditPageControl

枚举

public enum Inputtype
    {
        //,datetime-local
        text, password, datetime, date, month, time, week, number, email, url, search, tel, color
    }
    public enum ItemAlign
    {
        Left,
        Right,
        Top,
        Bottom
    }
    /// <summary>
    /// 验证
    /// </summary>
    public enum VerificationStr
    {
        /// <summary>
        /// 是否为空
        /// </summary>
        isnull,
        /// <summary>
        /// 电话号码验证
        /// </summary>
        phone,
        /// <summary>
        /// 密码验证
        /// </summary>
        password,
        /// <summary>
        /// 邮箱验证
        /// </summary>
        email,
        /// <summary>
        /// 字母和数字
        /// </summary>
        Lettersandnumbers,
        /// <summary>
        /// 字母
        /// </summary>
        Letters,
        /// <summary>
        /// 数字
        /// </summary>
        intnumbers,
        /// <summary>
        /// 浮点数
        /// </summary>
        decmilnumber,
        /// <summary>
        /// 时间验证2007-06-05 10:57:10
        /// </summary>
        checkFullTime,
        /// <summary>
        /// 时间验证 10:57:10
        /// </summary>
        checkTime,
        /// <summary>
        /// 时间验证  2007-06-05
        /// </summary>
        checkDate,
        /// <summary>
        /// 身份证号码验证
        /// </summary>
        card,
        /// <summary>
        /// 整型数据验证
        /// </summary>
        checkInteger,
        /// <summary>
        /// 固定电话号码 验证
        /// </summary>
        checkTelephone,
        /// <summary>
        /// QQ号码验证
        /// </summary>
        checkQQ,
        /// <summary>
        /// 外部链接地址验证
        /// </summary>
        checkURL,
        /// <summary>
        /// 金额类型
        /// </summary>
        isMoney
}

    public enum ContorlType
    {
        /// <summary>
        /// 下拉列表
        /// </summary>
        select,
        /// <summary>
        /// 编辑器
        /// </summary>
        uedit,
        /// <summary>
        /// 文件上传
        /// </summary>
        fileUpload,
        /// <summary>
        /// 文本域
        /// </summary>
        textarea,
        /// <summary>
        /// 复选框
        /// </summary>
        checkbox,
        /// <summary>
        /// 设置字体颜色
        /// </summary>
        FontColor,
        /// <summary>
        /// 多文件上传
        /// </summary>
        fileUploadmore
    }
枚举

可能会用到的配置文件AppSettings.config

 

<?xml version="1.0" encoding="utf-8"?>
<appSettings>
<!--设置自动生成表单样式-->
  <!-- <input name="S_PROJECTCODE" type="text" id="S_PROJECTCODE" tipmsg="招标项目编号" class="mediuminput">-->
  <!--mediuminput-->
  <!--bootstrap表单设置  input:form-control-->
  <!--下拉框-->
  <add key="select" value=""/>
  <!--文件上传-->
  <add key="fileUpload" value=""/>
  <!--多行文本框-->
  <add key="textarea" value=""/>
  <!--编辑器-->
  <add key="uedit" value=""/>
  <!--复选框-->
  <add key="checkbox" value=""/>
  <!--字体颜色-->
  <add key="FontColor" value=""/>
  <!--多文件上传-->
  <add key="fileUploadmore" value=""/>
  <add key="input" value=""/>
  <!--每项格式-->
  <!--<div class="listitem form-group">
    <label class="listleft titles">招标项目编号:</label>
    <div class="listleft con">
      <input name="S_PROJECTCODE" type="text" id="S_PROJECTCODE" tipmsg="招标项目编号" class="mediuminput">
    </div>
    <span style="color:red" class="errorlog">*</span>
  </div>-->
  <!--表单其他样式-->
  <add key="boxdiv" value="listitem form-group"/>
  <add key="boxChildlabel" value="listleft titles"/>
  <add key="boxChilddiv" value="listleft con"/>
  <!--bootstrap表单设置-->
  <!--<add key="boxdiv" value="form-group"/>
  <add key="boxChildlabel" value=""/>
  <add key="boxChilddiv" value="form-control"/>-->
</appSettings>
View Code

注意:代码不是很完善  对于复选框、字体颜色还需加强构思,学识不够渊博 可能写得有点烂

适合于多表多字段需要快速开发完成的项目

 

posted @ 2017-11-24 13:39  小神猪  阅读(571)  评论(0编辑  收藏  举报