RadioButtonList value is null

125 Views Asked by At

I am building an a website for multiple choices. My page contains two RadioButtonLists that I generate when the page is loaded. However, when I choose item, and click the button, the selectedItem has null values. This is my code:

public partial class WebForm1 : System.Web.UI.Page
{
    MultipleChoiceDbContext db = new MultipleChoiceDbContext();
    List<string> questionList = new List<string>();
    List<RadioButtonList> optionList = new List<RadioButtonList>();
    List<string> answerList = new List<string>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var query = from data in db.QAs
                        select data;
            QA question;

            int i = 1;
            ListItem liA;
            ListItem liB;
            ListItem liC;
            ListItem liD;
            while (i < 3)
            {
                question = query.ToList()[(new Random()).Next(query.ToList().Count)];
                questionList.Add(question.Question);
                answerList.Add(question.Answer);

                liA = new ListItem(question.A, question.A);
                liB = new ListItem(question.B, question.B);
                liC = new ListItem(question.C, question.C);
                liD = new ListItem(question.D, question.D);

                RadioButtonList rdl = new RadioButtonList();
                rdl.Items.Add(liA);
                rdl.Items.Add(liB);
                rdl.Items.Add(liC);
                rdl.Items.Add(liD);

                optionList.Add(rdl);
                phContent.Controls.Add(rdl);

                i++;
            }
        }
    }
    protected void btnok_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < optionList.Count; i++)
        {
            string content = "";
            content += optionList[i].SelectedItem.Value + "<br />";
            Response.Write(content);

        }
    }
}
0

There are 0 best solutions below