C# ASP.NET 中的动态表单生成
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/792278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic Form Generation in ASP.NET
提问by Joshscorp
I would like to dynamically generate a form from a database in ASP.NET, what is the best approach? Are there any built in functionalities I can use?
我想从 ASP.NET 中的数据库动态生成表单,最好的方是什么?我可以使用任何内置功能吗?
I will have database tables to represent the panels and their names, then for each panels, it contains the different fields and their types (Combos, Textboxes, etc..).
我将有数据库表来表示面板及其名称,然后对于每个面板,它包含不同的字段及其类型(组合、文本框等)。
Please advice, thank you.
请指教,谢谢。
Note: I have to use Telerik Ajax controls for the form generation
注意:我必须使用 Telerik Ajax 控件来生成表单
采纳答案by Damovisa
Have a look at Dynamic Data.
看看动态数据。
I recently found out about it and it's already saved me a lotof time.
我最近发现了它,它已经为我节省了很多时间。
Update:
更新:
Apologies - having reread the question, I don't think this is what you were after.
道歉 - 重读这个问题后,我认为这不是你想要的。
If you want to dynamically generate the form based on the records in your database, you may have to write your own engine.
如果要根据数据库中的记录动态生成表单,则可能需要编写自己的引擎。
A couple of suggestions though:
不过有几个建议:
- I'd look at using reflection to load controls rather than large case statements. That way you could dynamically add different control types just by including the new assembly. You wouldn't have to write new code.
- Make sure you include a way to control the display order in your database. I note that you want to use a different table for each panel of controls. I'd advise against that because of the display order problem. If you have a table with a list of panels and a table with a list of data items + foreign key references to the panels, you'll be able to order them in a predictable and controllable way on the page.
- 我会考虑使用反射来加载控件而不是大型 case 语句。这样您就可以通过包含新程序集来动态添加不同的控件类型。您不必编写新代码。
- 确保包含一种控制数据库中显示顺序的方。我注意到您希望为每个控件面板使用不同的表格。由于显示顺序问题,我建议不要这样做。如果您有一个包含面板列表的表格和一个包含数据项列表 + 面板的外键引用的表格,您将能够在页面上以可预测和可控的方式对它们进行排序。
Update: more info on reflection
更新:关于反射的更多信息
Put simply, reflection is when you find out about details of an assembly at runtime. In this case, I suggest using reflection to load a control based on the information in your database.
简而言之,反射是当您在运行时发现程序集的详细信息时。在这种情况下,我建议使用反射根据数据库中的信息加载控件。
So if you had a record in your database similar to the following:
因此,如果您的数据库中有类似于以下内容的记录:
FieldName DataType DisplayControl DisplayProperty
----------------------------------------------------------------------------------
FirstName System.String System.Web.UI.WebControls.TextBox Text
You could use some code like the following to generate the control on the page (note that it's untested):
您可以使用如下代码在页面上生成控件(请注意,它未经测试):
// after getting the "PageItem" database records into a "pageItems" array
foreach (PageItem p in pageItems)
{
// get the type and properties
Type controlType = System.Type.GetType(p.DisplayControl)
PropertyInfo[] controlPropertiesArray = controlType.GetProperties();
// create the object
object control = Activator.CreateInstance(controlType);
// look for matching property
foreach (PropertyInfo controlProperty in controlPropertiesArray)
{
if (controlPropertiesArray.Name == p.DisplayProperty)
{
// set the Control's property
controlProperty.SetValue(control, "data for this item", null);
}
}
// then generate the control on the page using LoadControl (sorry, lacking time to look that up)
There is a really good page outlining how to do this here. It looks to be pretty much what you're after.
有一个非常好的页面概述了如何在此处执行此操作。它看起来几乎是你所追求的。

