简体   繁体   中英

How to add a value to a RadioButtonList

Can anyone show how I would add a value of 0 to the wrong answers and aa value of 1 to the correct one?

protected void Page_Load(object sender, EventArgs e)
{   
    //Question
    lblQuestion1.Text = "Which of the following is not a country in Europe?";

    //Answers
    lstAns1.Items.Add("Enland");
    lstAns1.Items.Add("Germany");
    lstAns1.Items.Add("France");
    lstAns1.Items.Add("Canada");
}

I think you may add ListItems instead of only text.

protected void Page_Load(object sender, EventArgs e)
{   
    if (!Page.IsPostBack)
    {
        //Question
        lblQuestion1.Text = "Which of the following is not a country in Europe?";

        //Answers
        lstAns1.Items.Add(new ListItem("Enland", "0"));
        lstAns1.Items.Add(new ListItem("Germany", "0"));
        lstAns1.Items.Add(new ListItem("France", "0"));
        lstAns1.Items.Add(new ListItem("Canada", "1"));
    }
}

I have added a check if this page is loaded by HTTP Get or by HTTP Post (see: Page.IsPostBack ).

Anyway - this code is completely static.... You may think it over once again.

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