string result = "";
foreach(ListItem li in this.RadioButtonList1.Items)
{
if(li.Selected)
{
result = li.Text;
Label.Text = "提示:你選擇的答案是" + result;
}
}
if(result != "")
{
if(result == "正確答案")
{
Label.Text = "提示:正確";
}
else
{
Label.Text = "提示:錯誤";
}
}
else
{
Label.Text = "提示:你沒有選擇答案";
}
隨手寫的。

解决方案 »

  1.   

     protected void Button1_Click(object sender, EventArgs e)
            {
               
                foreach (DataListItem item in DataList1.Items)
                {
                    RadioButtonList rbl2 = (RadioButtonList)item.FindControl("RadioButtonList1");
                    if (rbl2.SelectedIndex != -1)
                    {
    this.HiddenField1.Value = rbl2.SelectedValue.ToString();
          }                else
                    {
                        Response.Write("<script>alert('对不起,请选择答案!')</script>");
                        return;
                    }
                }
      int i = (int)ViewState["Scount"];
                            i ++;
                            ViewState["Scount"] = i;
                            SqlConnection mycon = new SqlConnection("Server=(local);Database=takeawaylanguages;UID=sa;Password=sa123");
                            mycon.Open();
                            string str = "select * from exam where examid=" + this.PartId + " and id=" +ViewState["Scount"] + "";
                            SqlCommand mycmd = new SqlCommand(str, mycon);
                            SqlDataReader reader;
                            reader = mycmd.ExecuteReader();
                           
                           
                  if(reader.HasRows)
                  {
                        Response.Write(HiddenField1.Value);
                       
                                DataList1.DataSource = reader;
                                DataList1.DataBind();
                               // mycon.Close();
                    }
                          
                            else
                            {
                              
                                Response.Write(bid);
                                DataList1.Visible = false;
                                Button1.Visible = false;                            Panel1.Visible = true;
                            }
    }
    我想在这个单击事件中判断 把答案要绑定到一个DATALIST中用来显示正确答案和已选的答案