动态 ASP.net Web 表单
我正在创建一个 ASP.NET 应用程序,程序管理员可以在其中为用户填写和提交的表单定义自定义字段。
管理员需要定义各种类型的字段,例如复选框、单选按钮、文本框和文本区域供用户填写。 管理员还可以定义是否需要这些自定义字段。
我现在正处于规划阶段,想知道如何将这些自定义字段定义存储在数据库中,以及如何呈现它们并使它们发挥作用。
编辑
最终用户使用这些动态创建的表单字段提交的表单数据也必须保留在数据库中。
我该如何解决这个问题?
I am creating an ASP.NET application in which the administrator of the program will be able to define custom fields for forms that the users will fill out and submit.
The admin needs to define various types of fields like checkboxes, radio buttons, textboxes, and textareas for the user to fill out. The admin also can define whether these custom fields should be required.
I am in the planning stages now and am wondering about how I will store these custom field definitions in the database and how I will render them out and make them function.
EDIT
The form data that is submitted by end users with these dynamically created form fields also has to be persisted in the database.
How can I tackle this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这是我快速想出的用于概念验证的东西。
DynamicForms.zip
我在 VS 2008 SP1 中构建。 appdata 文件夹中有一个 sql server 数据库,因此如果您想运行它,sqlexpress 将有所帮助。
我很快就构建了这个,所以它非常草率。
Here is something I have whipped up real quick for proof of concept.
DynamicForms.zip
I built in VS 2008 SP1. There is a sql server db in the appdata folder, so sqlexpress will help if you want to run this.
I built this quickly, so it's pretty sloppy.
如果没有完成此操作或类似的操作,我认为您的数据库结构可能包括控件的类型、其名称、其值类型、是否经过验证、如何验证、是否需要。 然后,当您阅读所需控件的记录/数据集时,您可能会将记录集中的控件类型与翻译后的值进行比较,然后将其添加到 Web 表单中。
例如:
理论上这会将标签放在文本框控件旁边。 我不知道 Div 是否有效,但您可以对 CheckBox、TextBox、Label 等运行此类操作。
一旦控件和标签位于页面上,您将需要某种形式的操作来导致服务器将值保存到数据库中。 我建议将表和列名称与对象列表一起存储。 然后将表单字段映射到数据库列以进行插入。 (记住这个解释就像在 200,000 英尺处)
Without having done this or really anything like it, I would think your DB structure might include the type of control, its name, its value type, if its validated, how its validated, if its required. Then as you are reading through the records/dataset of controls desired, you would probably compare the control type in the recordset to the translated value and then add it to the webform.
For example:
This in theory would put the label next to the textbox control. I have no idea if the Div would work, but you can run through this type of thing for CheckBox, TextBox, Label, etc.
Once the controls and labels are on the page, you'll want some form of action to cause the server to save the values to a DB. I'd recommend storing the table and column name with the list of objects. Then map the form field over to a DB column for the insert. (keep in mind this explanation is like at 200,000 feet)
可能需要以下基本结构:Form、Field、FieldType。 您将需要每个业务对象和数据表,以便可以存储每个字段的配置。
您可能需要该字段具有多个可以设置的属性(例如必需的),以便您可以独立跟踪每个字段。
然后,您将需要某种可以读取数据层的实用程序,然后对于每个字段和字段类型知道要创建什么类型的控件,以及如何将控件添加到当前页面。
在主页上使用 asp:PlaceHolder 标记将允许您使用表单构建实用程序动态地将控件添加到此占位符。
最后,您可能希望为生成的表单的不同部分构建一个包含多个类的样式表,以便客户端可以自定义您生成的任何控件表的字体、间距等。
编辑:
如果需要保留在表单上输入的数据,那么您需要决定是否想要有一个可自定义的数据层。 您可以拥有一个空数据库表,在该表上使用 ALTER TABLE 脚本以允许客户端根据需要向数据库添加列,然后您可以设置哪个控件绑定到哪个列。 控件与数据库列之间的映射对于正确存储数据至关重要。
The following basic structure will likely be needed: Form, Field, FieldType. You'll need business objects and data tables for each so you can store the configurations for each field.
You'll likely need the field to have several properties that can be set (like being required) so that you can track this independently for each field.
You'll then need some sort of utility that can read the data layer, and then for each field and field type know what type of control to make, and how to add the controls to the current page.
Using an asp:PlaceHolder tag on the main page will allow you to dynamically add controls to this place holder using your form building utility.
Finally, you'll likely want to build a style sheet with several classes for different portions of the form that are generated so that the client can customize the fonts, spacing, etc. of whatever table of controls you generate.
EDIT:
If the data entered on the form needs to be persisted, then you need to decide if you want to have a customizable data layer. You can have an empty database table that you use ALTER TABLE scripts on to allow the client to add columns to the database as needed, and then you can set which control binds to which column. This mapping between control to database column will be crucial to storing the data correctly.
每个控件都可以实例化并添加到页面中。
示例:
之后,您应该将控件添加到任何容器的 Controls 属性中:
要在回发后检索控件,您可以使用 FindControl 方法。
Every control can be instantiated and added to the page.
Example:
After that you should add the control to the Controls property of any container:
To retrieve the control after a postback you can use the method FindControl.
正如其他人所说,肯定有一些方法可以通过在运行时向 ASP.NET 页面动态添加控件来实现此目的。
或者,虽然我没有使用过 Microsoft Infopath,但据我所知听说过它,它可以完成您想要做的很多事情(即允许超级用户设计表格/调查问卷并放在网络上并随后收集数据)
如果您的客户有适当的 Microsoft Office 版本,他们可能已经有了 InfoPath。
As others have said, there are definately ways of doing this by dynamically adding controls to an ASP.NET page at runtime.
Alternatively, although I havent used Microsoft Infopath, from what I've heard about it, it does quite a lot of what you are trying to do (i.e. allow super-users to design forms/questionnaires and put on the web and gather the data back in afterwards)
If your client has the appropriate Microsoft Office edition, they might have InfoPath already.
我以前做过这个工作。
我为此使用了两个表,比方说
KeyField_Master:对于字段名称,类型,它是否是强制性的?
KeyField_Details
:根据(值和描述)存储自定义字段值。
使用 Page_Load 事件创建该字段
另外,您可以参考该问题“如何动态验证创建控件?”
这个问题与那个程序有关吗?
另外,验证可以是客户端的,如上面问题中所述?
或服务器端,通过将创建的字段存储在列表中,然后您可以在提交时检查该列表。
I worked on this before.
I used two tables for this, let's say
KeyField_Master: for field name, type ,and is it mandatory or not?
and
KeyField_Details: to store custom fields value in term of (value and description).
Use Page_Load event to create that fields
Also, you could refer to that question "How to validate dynamically created controls?"
This question is related to that procedure?
Also, validation could be client-side as described in above question?
or server-side by storing created fields in a list then you could check that list in time of submit.