在 ASP.NET Web 表单中创建动态 UI [英] Creating dynamic UI in ASP.NET web forms

查看:21
本文介绍了在 ASP.NET Web 表单中创建动态 UI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用从数据库中读取的以下结构创建一个调查页面.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆