【发布时间】:2010-10-21 23:50:26
【问题描述】:
我想从 ASP.NET 中的数据库动态生成表单,最好的方法是什么?有没有我可以使用的内置功能?
我将有数据库表来表示面板及其名称,然后对于每个面板,它包含不同的字段及其类型(组合、文本框等)。
请指教,谢谢。
注意:我必须使用 Telerik Ajax 控件来生成表单
【问题讨论】:
标签: c# asp.net html dynamic-data
我想从 ASP.NET 中的数据库动态生成表单,最好的方法是什么?有没有我可以使用的内置功能?
我将有数据库表来表示面板及其名称,然后对于每个面板,它包含不同的字段及其类型(组合、文本框等)。
请指教,谢谢。
注意:我必须使用 Telerik Ajax 控件来生成表单
【问题讨论】:
标签: c# asp.net html dynamic-data
看看Dynamic Data。
我最近发现了这件事,它已经为我节省了很多 时间。
更新:
道歉 - 重读了这个问题,我认为这不是你想要的。
如果您想根据数据库中的记录动态生成表单,您可能需要编写自己的引擎。
不过有几个建议:
更新:关于反射的更多信息
简单地说,反射就是在运行时发现程序集的细节。在这种情况下,我建议使用反射来根据数据库中的信息加载控件。
因此,如果您的数据库中有类似以下的记录:
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)
有一个非常好的页面概述了如何做到这一点here。它看起来几乎就是你所追求的。
【讨论】: