这是一个向用户提出多项选择问题的项目。
共有5个表:
我设法加载问题和答案。我也能够将数据存储到结果表中,但是我无法存储用户的答案。我想要的是当用户选择单选按钮时,答案的ID传递给result_detail表
以下是代码:
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
If RadioButton1.Checked = True Or RadioButton2.Checked = True Or RadioButton3.Checked = True Or RadioButton4.Checked = True Then
If Nombor.Text = 10 Then
Response.Redirect("EndQues.aspx")
Else
Nombor.Text = Val(Nombor.Text) + 1
Counter += 1
question()
clean()
End If
Else
MsgBox("Error", MsgBoxStyle.Exclamation, "Error")
End If
End Sub
Sub question()
conn.Open()
Dim cmd As New SqlCommand("Select * From view_question Where QuestionID=@IdSoalan", conn)
cmd.Parameters.AddWithValue("@IdSoalan", Counter)
Dim dr1 As SqlDataReader
dr1 = cmd.ExecuteReader
If dr1.Read() Then
Me.lblquest.Text = dr1("QuestionTxt")
Me.RadioButton1.Text = dr1("1")
Me.RadioButton2.Text = dr1("2")
Me.RadioButton3.Text = dr1("3")
Me.RadioButton4.Text = dr1("4")
Else
conn.Close()
Counter += 1
question
End If
conn.Close()
End Sub
Sub clean()
RadioButton1.Checked = False
RadioButton2.Checked = False
RadioButton3.Checked = False
RadioButton4.Checked = False
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
question()
conn.Open()
Dim cmd1 As New SqlCommand("Insert into TblResult (Result_Quiz_Id,Result_NoMatric,) values (1,'" & Session("NoMatrik") & "')", conn)
cmd1.ExecuteNonQuery()
conn.Close()
End Sub
答案 0 :(得分:0)
我在这里理解的是,您希望选定的单选按钮具有与答案相当的ID吗?我的解决方案是每次添加radiobutton文本时,将ID存储到4个元素的数组中。
Me.RadioButton1.Text = dr1("1")
myArr(0) = dr1("id_1")
Me.RadioButton2.Text = dr1("2")
myArr(1) = dr1("id_2")
Me.RadioButton3.Text = dr1("3")
myArr(2) = dr1("id_3")
Me.RadioButton4.Text = dr1("4")
myArr(3) = dr1("id_4")
然后每次你保存它,只需设置条件来识别所选的单选按钮。
If Me.RadioButton1.Checked = True Then
answerID = myArr(0)
等等。还有另一种方法,但如果这是您需要的过程,请首先尝试此方法。