Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
183 views
in Technique[技术] by (71.8m points)

c# - Dynamic Form Generation in 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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Have a look at Dynamic Data.

I recently found out about it and it's already saved me a lot of 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.

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...