简体   繁体   中英

Create dynamic radiobuttonlist from object properties

I have a list of objects of multiple choice questions. I need to create a RadioButtonList with the object properties: 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 Structure:

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

    </ItemTemplate>
</asp:DataList>

Okay, try modelling SQuestion to match the following:

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
}

So now you can bind the choices to a drop down list. I would recommend using the onDataBinding method or onDataBound (its one of them, off the top of my head I'm not sure) and you can then bind the inner choice drop down list with the current question.Choices. That should work.

Hope this helps point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM