问题描述
我需要从数据库中读取以下结构创建一个调查页面。
I need to create a survey page with the following structure read from database.
Survey QuestionA
a) Answer1 [Radio button]
b) Answer2 [Radio button]
c) Answer3 [Radio button]
d) Answer4 [Radio button]
repeats..
该页面有一个需要动态地添加了许多问题。我需要在表格的结果存储在问题对象上提交的数组。我知道这样做的一个方法是在表中创建动态UI,并获得由的FindControl
的值。有没有更好的(优雅的)的方式来做到这一点?
The page has many questions that needs to be dynamically added. I need to store the result of the form on in an array of Question object on submit.One way I know to do this is create dynamic UI in a table and get the values by FindControl
.Is there a better (elegant) way to do this?
推荐答案
我建议创建一个 userconrol 实现的问题(标签)和答案(单选按钮),并且每个控件(标签,收音机)被绑定到你的用户控件的属性,然后你可以读取数据库的问题,并为每个数据创建这个用户控件的对象并设置对应的数据为用户控件的财产,并以读此状态下做相反的控制数据。
I suggest create a userconrol that implement a question(label) and answers(radio buttons) and each controls(labels,radios) is binded to a property of your usercontrol, Then you can read questions from database and for each data create this usercontrol object and set correspond data to that property of usercontrol, And to read data from control this state doing vice versa.
尽管你必须重新在每个回传用户控件并设置为默认的数据对这些。
Albeit you must recreate usercontrols in each post back and set default data to those.
你也可以创建多个用户控件是不同的UI 的继承接口等 IQuestion 和工厂创造每个用户控件的类取决于环境的可变因素。
Also you can create multiple usercontrols with different UI that inherit a interface such as IQuestion, and a factory class that create each of usercontrols depend of environment varibles.
这篇关于在ASP.NET Web表单创建动态UI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!