分类

链接

2018 年 1 月
1234567
891011121314
15161718192021
22232425262728
293031  

近期文章

热门标签

新人福利,免费薅羊毛

现在位置:    首页 > .NET > 正文
共享办公室出租
 FormDiy.NET自定义表单框架(二):动态创建控件
.NET 暂无评论 阅读(443)
  1.    // 动态创建控件
  2.         // 注意:这段代码需要每次加载页面都执行,因此不能放在 if(!IsPostBack) 逻辑判断中
  3.         protected void Page_Init(object sender, EventArgs e)
  4.         {
  5.  
  6.             var fieldList = db.Fields.Where(=> f.FormId == 1).OrderBy(=> f.Order).ToList();
  7.             foreach (var field in fieldList)
  8.             {
  9.                 Panel panel = new Panel();
  10.                 panel.ID = "panel" + field.Id;
  11.                 panel.ShowBorder = false;
  12.                 panel.ShowHeader = false;
  13.                 panel.Margin = "0 0 5px 0";
  14.                 panel.Layout = Layout.Column;
  15.                 var ctrl = GetControl(field);
  16.                 if (ctrl != null)
  17.                 {
  18.                     panel.Items.Add(ctrl);
  19.                 }
  20.                 Form1.Items.Insert(Form1.Items.Count - 1, panel);
  21.  
  22.             }
  23.  
  24.  
  25.  
  26.  
  27.         }
  28.  
  29.         private ControlBase GetControl(Db.Field field)
  30.         {
  31.             ControlBase ctrl = null;
  32.  
  33.             Enum.FieldType type = (Enum.FieldType)field.Type;
  34.             switch (type)
  35.             {
  36.                 case FormDiy.Enum.FieldType.TextBox:
  37.                     TextBox txt = new TextBox();
  38.                     txt.EmptyText = field.Tip;
  39.                     txt.Width = field.Width;
  40.                     txt.Label = field.Title;
  41.  
  42.                     ctrl = txt;
  43.                     break;
  44.                 case FormDiy.Enum.FieldType.TextArea:
  45.                     break;
  46.                 case FormDiy.Enum.FieldType.Editor:
  47.                     HtmlEditor editor = new HtmlEditor();
  48.                     editor.Width = field.Width;
  49.                     editor.Height = field.Height;
  50.                     editor.Label = field.Title+":";
  51.                     editor.LabelSeparator = "";
  52.                     ctrl = editor;
  53.                     break;
  54.                 case FormDiy.Enum.FieldType.RadioBoxList:
  55.                     break;
  56.                 case FormDiy.Enum.FieldType.CheckBoxList:
  57.                     break;
  58.                 case FormDiy.Enum.FieldType.DrawDownList:
  59.                     break;
  60.                 case FormDiy.Enum.FieldType.Upload:
  61.                     break;
  62.                 case FormDiy.Enum.FieldType.MutiUpload:
  63.                     break;
  64.                 case FormDiy.Enum.FieldType.DatePicker:
  65.                     break;
  66.                 case FormDiy.Enum.FieldType.Tree:
  67.                     break;
  68.                 case FormDiy.Enum.FieldType.Advanced:
  69.                     break;
  70.                 case FormDiy.Enum.FieldType.Hidden:
  71.                     break;
  72.                 default:
  73.                     break;
  74.             }
  75.             if (ctrl == null)
  76.             {
  77.                 return null;
  78.             }
  79.             ctrl.ID = field.Name;
  80.             return ctrl;
  81.         }

============ 欢迎各位老板打赏~ ===========

本文版权归Bruce's Blog所有,转载引用请完整注明以下信息:
本文作者:Bruce
本文地址: FormDiy.NET自定义表单框架(二):动态创建控件 | Bruce's Blog

发表评论

留言无头像?