开发者

ASP.NET Dynamic form generation and submition

开发者 https://www.devze.com 2022-12-17 00:04 出处:网络
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 t

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 (e.g. nested collections, autocomplete, datepickers etc.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号