8
我想從ASP.NET中的數據庫動態生成表單,最好的方法是什麼?是否有任何內置的功能可以使用?ASP.NET中的動態表單生成
我將有數據庫表來表示面板及其名稱,然後對於每個面板,它包含不同的字段及其類型(Combos,Textboxes等)。
請指教,謝謝。
注:我不得不使用Telerik的Ajax控件的形式代
我想從ASP.NET中的數據庫動態生成表單,最好的方法是什麼?是否有任何內置的功能可以使用?ASP.NET中的動態表單生成
我將有數據庫表來表示面板及其名稱,然後對於每個面板,它包含不同的字段及其類型(Combos,Textboxes等)。
請指教,謝謝。
注:我不得不使用Telerik的Ajax控件的形式代
看一看Dynamic Data。
我最近發現了它,它已經爲我節省了lot的時間。
更新:
道歉 - 有重讀的問題,我不認爲這是你所追求的。
如果要根據數據庫中的記錄動態生成表單,則可能必須編寫自己的引擎。
一對夫婦的建議,但:
更新:對反射
簡而言之更多信息,反思是當你瞭解在運行時組件的細節。在這種情況下,我建議使用反射來根據數據庫中的信息加載控件。
所以,如果你有在你的數據庫類似如下的記錄:
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。它看起來幾乎是你所追求的。
我不熟悉反思,你能擴展你的意思嗎?最好用一個簡單的例子? – Joshscorp 2009-04-27 05:16:31