【问题标题】:ASP.net - Dynamically displaying controlsASP.net - 动态显示控件
【发布时间】:2010-01-07 09:48:25
【问题描述】:

我正在开发测验项目。在 DetailsView 中我想显示问题和答案。

答案的数量因问题而异。举例说明

1 ) C# Support

   (i)  Generics
   (ii) LINQ
   (iii)EntLib  

2 ) Find the odd one
    (i)  DB2
    (ii) Oracle
    (iii)MS-Access 
    (iv) Sql Server
    (v)  Javascript

所以我无法修复单选按钮的数量。有些问题可能有多个答案。所以我需要显示复选框而不是单选按钮。

我的问题是如何动态生成单选按钮或复选框?

【问题讨论】:

    标签: asp.net data-binding


    【解决方案1】:

    为每个问题创建一个 RadioButtonList 或 CheckBoxList 并使用其 Items 集合添加答案。

    非常简单的 C# 示例:

    class Question
    {
        public string QuestionText;
        public List<string> Answers;
    }
    
    protected void AddQuestionsToContainer(Control container, List<Question> questions)
    {
        foreach (Question q in questions)
        {
            var qt = new Label();
            qt.Text = q.QuestionText;
            container.Controls.Add(qt);
    
            var rbl = new RadioButtonList();
    
            foreach (string answer in q.Answers)
            {
                rbl.Items.Add(new ListItem(answer));
            }
    
            container.Controls.Add(rbl);
        }
    }
    

    【讨论】:

      【解决方案2】:

      我认为您的问题更具体地说是如何确定哪个测验问题有多个答案。

      如果我是正确的,那么您需要在 DB 的表中添加一个像 isMultipleAnswers BIT 这样的额外列(或者您需要为每个问题设置一个标志的任何来源),并为 DetailView 处理一个事件,例如DataBinding,检查该字段的值,基于添加 RadioButtonList 或 CheckBoxList。

      希望这会有所帮助!

      顺便说一句,你为什么不使用中继器??

      【讨论】:

        猜你喜欢
        • 2012-05-10
        • 1970-01-01
        • 1970-01-01
        • 2011-06-28
        • 2012-01-09
        • 1970-01-01
        • 1970-01-01
        • 2013-01-04
        • 1970-01-01
        相关资源
        最近更新 更多