时间:2021-07-01 10:21:17 帮助过:17人阅读
新增编辑加载页面(改页面需要继承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";//设置初始值