我想从 ASP.NET 中的数据库动态生成表单,最好的方法是什么?我可以使用任何内置功能吗?
我将有数据库表来表示面板及其名称,然后对于每个面板,它包含不同的字段及其类型(组合、文本框等)。
请指教,谢谢。
注意:我必须使用 Telerik Ajax 控件来生成表单
我想从 ASP.NET 中的数据库动态生成表单,最好的方法是什么?我可以使用任何内置功能吗?
我将有数据库表来表示面板及其名称,然后对于每个面板,它包含不同的字段及其类型(组合、文本框等)。
请指教,谢谢。
注意:我必须使用 Telerik Ajax 控件来生成表单
看看动态数据。
我最近发现了它,它已经为我节省了很多时间。
更新:
道歉 - 重读了这个问题,我认为这不是你所追求的。
如果要根据数据库中的记录动态生成表单,则可能必须编写自己的引擎。
不过有几点建议:
更新:有关反射的更多信息
简而言之,反射是您在运行时发现程序集的详细信息。在这种情况下,我建议使用反射来根据数据库中的信息加载控件。
因此,如果您的数据库中有类似于以下内容的记录:
FieldName DataType DisplayControl DisplayProperty
----------------------------------------------------------------------------------
FirstName System.String System.Web.UI.WebControls.TextBox Text
您可以使用如下代码在页面上生成控件(请注意,它未经测试):
// 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)
有一个非常好的页面概述了如何在此处执行此操作。它看起来几乎就是你所追求的。