Create a dynamic interface in ASP.NET web forms

I need to create a survey page with the following structure read from a database.

Survey QuestionA a) Answer1 [Radio button] b) Answer2 [Radio button] c) Answer3 [Radio button] d) Answer4 [Radio button] repeats.. 

There are many questions on the page that need to be dynamically added. I need to save the form result in an array of the Question object on submit. One of the ways I know this is to create a dynamic interface in the table and get the FindControl values. Is there a better (elegant) way to do this?

+4
source share
5 answers

In ASP.Net MVC, it handles the dirty work for you using the default middleware. Of course, you can also create your own. Although this does not give you the automatic solution that you hoped for in ASP.Net Web Forms, my preference in this situation is to follow the similar general pattern that ASP.Net MVC uses for this naming convention, which simplifies it. Then you can start writing code that can be reused over time. Here is a link to an article explaining the Haack blog naming convention http://haacked.com/archive/2008/10/23/model-binding-to-a-list.aspx .

A long-term recommendation is to turn to ASP.Net MVC, life is better here :)

+2
source

I suggest creating a userconrol that implements the question (label) and answers (switches), and each control (labels, radios) is attached to the property of your usercontrol, then you can read the questions from the database, and for this, create this usercontrol object and set the appropriate data to this usercontrol property, and to read data from this state, which runs the other way around.

Although you should recreate usercontrols in each column and set default data for them.

You can also create several user controls with a different user interface that inherit the interface , such as IQuestion , and the factory that create each user control depends on environment variables.

+2
source

You can use jQuery to get the selected radio buttons by the checked property and add them with the question number. for example for question 1 do you have

So, you can get the values, iterate through the radio buttons: $ ("Input [type = 'radio']”). Verified

0
source

You can always add controls programmatically.

Say you have an aspnet control panel that you can execute ...

 RadioButton rb = new RadioButton(); rb.ID = "rbRadioButton"; rb.Name = "rbRadioButton"; rb.cssClass = "radioClass"; Panel1.Controls.Add(rb); 

Sorry for the severity of the example without having received a visual studio at the moment to test it, but I hope you get this idea. You could essentially create the whole question this way, based solely on the database. The downside is getting the values, since you have to override the Render method (if I remember correctly, some time has passed, I have an example if you want me to find it).

I admit that this is a bit overkill, but it is a possible solution.

0
source

use the radioobuttonlist (which you can see in the toolbar, this is the standard asp.net control) for this purpose. It depends on your needs.

0
source

Source: https://habr.com/ru/post/1384903/


All Articles