FormDiy.NET自定义表单框架(二):动态创建控件
- // 动态创建控件
- // 注意:这段代码需要每次加载页面都执行,因此不能放在 if(!IsPostBack) 逻辑判断中
- protected void Page_Init(object sender, EventArgs e)
- {
- var fieldList = db.Fields.Where(f => f.FormId == 1).OrderBy(f => f.Order).ToList();
- foreach (var field in fieldList)
- {
- Panel panel = new Panel();
- panel.ID = "panel" + field.Id;
- panel.ShowBorder = false;
- panel.ShowHeader = false;
- panel.Margin = "0 0 5px 0";
- panel.Layout = Layout.Column;
- var ctrl = GetControl(field);
- if (ctrl != null)
- {
- panel.Items.Add(ctrl);
- }
- Form1.Items.Insert(Form1.Items.Count - 1, panel);
- }
- }
- private ControlBase GetControl(Db.Field field)
- {
- ControlBase ctrl = null;
- Enum.FieldType type = (Enum.FieldType)field.Type;
- switch (type)
- {
- case FormDiy.Enum.FieldType.TextBox:
- TextBox txt = new TextBox();
- txt.EmptyText = field.Tip;
- txt.Width = field.Width;
- txt.Label = field.Title;
- ctrl = txt;
- break;
- case FormDiy.Enum.FieldType.TextArea:
- break;
- case FormDiy.Enum.FieldType.Editor:
- HtmlEditor editor = new HtmlEditor();
- editor.Width = field.Width;
- editor.Height = field.Height;
- editor.Label = field.Title+":";
- editor.LabelSeparator = "";
- ctrl = editor;
- break;
- case FormDiy.Enum.FieldType.RadioBoxList:
- break;
- case FormDiy.Enum.FieldType.CheckBoxList:
- break;
- case FormDiy.Enum.FieldType.DrawDownList:
- break;
- case FormDiy.Enum.FieldType.Upload:
- break;
- case FormDiy.Enum.FieldType.MutiUpload:
- break;
- case FormDiy.Enum.FieldType.DatePicker:
- break;
- case FormDiy.Enum.FieldType.Tree:
- break;
- case FormDiy.Enum.FieldType.Advanced:
- break;
- case FormDiy.Enum.FieldType.Hidden:
- break;
- default:
- break;
- }
- if (ctrl == null)
- {
- return null;
- }
- ctrl.ID = field.Name;
- return ctrl;
- }
============ 欢迎各位老板打赏~ ===========
与本文相关的文章
- · The instance of entity type ‘Customer’ cannot be tracked because another instance with the same key value for {‘Id’} is already being tracked.
- · .NET8实时更新nginx ip地址归属地
- · 解决.NET Blazor子组件不刷新问题
- · .NET8如何在普通类库中引用 Microsoft.AspNetCore
- · .NET8 Mysql SSL error
- · ASP.NET Core MVC的Razor视图渲染中文乱码的问题
- · .NETCORE 依赖注入服务生命周期
- · asp.net zero改mysql
- · .NET5面试汇总
- · .Net连接Mysql数据库的Convert Zero Datetime日期问题
- · vue使用element-ui中的Message 、MessageBox 、Notification
- · Asp.Net Core Filter 深入浅出的那些事-AOP