从对象属性创建动态radiobuttonlist

时间:2012-07-25 23:49:13

标签: c# asp.net dynamic properties radiobuttonlist

我有一系列多项选择题的对象。我需要使用对象属性创建一个RadioButtonList:Choice_A,Choice_B,... Choice_D。

var qs = (from questions in dc.Survey_Questions
                  where questions.Survey_ID == surveyid                     
                  select new SQuestions
                  {
                      QuestionID = questions.Question_ID,
                      SurveyID = questions.Survey_ID,                         
                      Description = questions.Description,
                      Choice_A = questions.Choice_A,
                      Choice_B = questions.Choice_B,
                      Choice_C = questions.Choice_C,
                      Choice_D = questions.Choice_D,
                    }).ToList();
DataList dtQuestion.DataSource = qs;

HTML结构:

<asp:DataList ID="dtQuestion" runat="server" RepeatDirection="Vertical" >`
    <ItemTemplate>
        <%# Eval("Description") %> `          
    <ItemTemplate> 
    <RadioButtonList></RadiobuttonList>
    </ItemTemplate>

    </ItemTemplate>
</asp:DataList>

1 个答案:

答案 0 :(得分:0)

好的,尝试建模SQuestion以匹配以下内容:

public class SQuestion
{
    int QuestionId = 0; //int? change to whatever
    int SurveyId = 0; //same as above
    string Description = string.Empty;
    List<string> Choices = new List<string>(); //notice this is a list
}

现在您可以将选项绑定到下拉列表。 我建议使用onDataBinding方法或onDataBound(它中的一个,我不确定),然后你可以将内部选择下拉列表与当前问题绑定.Choices。这应该有用。

希望这有助于指明你正确的方向。