动态表单生成在ASP.NET [英] Dynamic Form Generation in ASP.NET

查看:83
本文介绍了动态表单生成在ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从ASP.NET数据库中动态生成的形式,什么是最好的方法呢?是否有任何内置的功能,我可以使用?

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?

我将数据库表重新present面板和他们的名字,则对于每个电池板,它包含不同的字段和它们的类型(连击,文本框等)。

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..).

请指教,谢谢。

请注意:我使用Telerik的Ajax控件的表单生成

Note: I have to use Telerik Ajax controls for the form generation

推荐答案

看一看动态数据

最近,我发现了它,它已经救了我的大量的时间

I recently found out about it and it's already saved me a lot of time.

更新:

道歉 - 有重读的问题,我不认为这是你所追求的。

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.

一对夫妇的建议,但:


  • 我想看看使用反射来加载控件,而不是大的case语句。这样,你就可以动态地只是包括新集添加不同的控制类型。你不会写入新的code。

  • 确保您有一种方法来控制你的数据库显示顺序。我注意到,您要使用一个不同的表用于控制每个面板。我建议反对,由于显示顺序的问题。如果你有面板的名单和数据项+的面板外键引用列表的表的表,你就可以命令他们在predictable在页面上可控的方式。

更新:对反射更多信息

简而言之,反射是当你了解在运行时程序集的详细信息。在这种情况下,我建议使用反射根据数据库中的信息加载控制。

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

您可以使用一些code像下面生成页面上的控制(注意,这是未经测试):

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)

有是一个非常好的网页概述如何此处做到这一点。它看起来是pretty您非常追求的。

There is a really good page outlining how to do this here. It looks to be pretty much what you're after.

这篇关于动态表单生成在ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆