简体   繁体   中英

ASP.NET Dynamic form generation and submition

I need to create a form where for each day in the month the user will enter in several different values (Im thinking a grid sort of thing, values along the top, day of the month along the side) using ASP.NET forms with C# backend...

So what would be the best way to generate this form? I dont want to hardcode that many controls as it would make the submition a little annoying but i also would need to be able to give it values entered in previous days and have it display them in the textboxes.

How would i go about creating this form and also how would i get the values from the textboxes in the backend?

Here is an example application that creates a dynamic form and submits it back to the database. It's written in VB.Net but im sure it's easily converted to C#.

http://www.asp101.com/samples/form_dynamic_aspx.asp

Basically the way it works is adding the controls to the form dynamically then accessing the values that are posted to the server with the Request.Form collection.

If you're building from scratch and can use the ASP.NET MVC framework, might be worth checking out the Dynamic Forms project over at codeplex: http://mvcdynamicforms.codeplex.com/

It might be overkill for your situation, but this appears a pretty powerful way to have a dynamic forms approach (you can store the config in an XML file, in a database etc), and posting the data back is handled by evaluating the Request object in server side code.

You can do this very easily using my FormFactory library.

By default it reflects against a view model to produce a PropertyVm[] array:

```

var vm = new MyFormViewModel
{
    OperatingSystem = "IOS",
    OperatingSystem_choices = new[]{"IOS", "Android",};
};
Html.PropertiesFor(vm).Render(Html);

```

but you can also create the properties programatically, so you could load settings from a database then create PropertyVm .

This is a snippet from a Linqpad script.

```

//import-package FormFactory
//import-package FormFactory.RazorGenerator


void Main()
{
    var properties = new[]{
        new PropertyVm(typeof(string), "username"){
            DisplayName = "Username",
            NotOptional = true,
        },
        new PropertyVm(typeof(string), "password"){
            DisplayName = "Password",
            NotOptional = true,
            GetCustomAttributes = () => new object[]{ new DataTypeAttribute(DataType.Password) }
        }
    };
    var html = FormFactory.RazorEngine.PropertyRenderExtension.Render(properties, new FormFactory.RazorEngine.RazorTemplateHtmlHelper());   

    Util.RawHtml(html.ToEncodedString()).Dump(); //Renders html for a username and password field.
}

```

Theres a demo site with examples of the various features you can set up (eg nested collections, autocomplete, datepickers etc.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM