我對如何編寫此代碼繪製空白。這是我現在所擁有的:根據字符串值修改程序流程
If queststg = "Q1" Then
q1a = vanswer
End If
If queststg = "Q2" Then
q2a = vanswer
End If
依此類推,直到我達到Q12。我可以弄清楚如何讓Q#(Q和數字)發生變化,但我無法弄清楚如何讓q#a發生變化。 q#一個變量名將始終是Q#加上a。
僅供參考 - 我有一個表Q1,Q2,...,Q12。 q#a告訴它將值vanswer(字段Q#)放在哪裏。上面的代碼有效,但我有12個問題,所以我不得不重複12次。必須有一個更簡單的方法。
爲了澄清什麼,我試圖做的,這裏的所有代碼(我只包括3個問題,以節省空間) -
Protected Sub btnGetResult_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim sb As New StringBuilder()
Dim commandtext As String
Dim q1a, q2a, q3a As Integer
Dim questno As String = 1 'first question number for field name
For Each item As RepeaterItem In Repeater1.Items
'find question
Dim lblquestion As Label = DirectCast(item.FindControl("lblQuestion"), Label)
Dim vanswer As String = String.Empty
'Count q for question number
Dim queststg As String = "Q" & questno
'Using FindControl method to find the radiobuttons
Dim rbnStronglyAgree As RadioButton = DirectCast(item.FindControl("rbnStronglyAgree"), RadioButton)
Dim rbnAgree As RadioButton = DirectCast(item.FindControl("rbnAgree"), RadioButton)
Dim rbnNeutral As RadioButton = DirectCast(item.FindControl("rbnNeutral"), RadioButton)
Dim rbnDisagree As RadioButton = DirectCast(item.FindControl("rbnDisagree"), RadioButton)
Dim rbnStronglyDisagree As RadioButton = DirectCast(item.FindControl("rbnStronglyDisagree"), RadioButton)
'Check which radiobutton is checked
If rbnStronglyAgree.Checked Then
Dim hid1 As HiddenField = DirectCast(item.FindControl("HiddenField1"), HiddenField)
vanswer = hid1.Value
ElseIf rbnAgree.Checked Then
Dim hid2 As HiddenField = DirectCast(item.FindControl("HiddenField2"), HiddenField)
vanswer = hid2.Value
ElseIf rbnNeutral.Checked Then
Dim hid3 As HiddenField = DirectCast(item.FindControl("HiddenField3"), HiddenField)
vanswer = hid3.Value
ElseIf rbnDisagree.Checked Then
Dim hid4 As HiddenField = DirectCast(item.FindControl("HiddenField4"), HiddenField)
vanswer = hid4.Value
ElseIf rbnStronglyDisagree.Checked Then
Dim hid5 As HiddenField = DirectCast(item.FindControl("HiddenField5"), HiddenField)
vanswer = hid5.Value
End If
sb.AppendLine(String.Format("<br />Q: {0} <br />A: {1} <br />", lblquestion.Text, vanswer))
**If queststg = "Q1" Then
q1a = vanswer
End If
If queststg = "Q2" Then
q2a = vanswer
End If
If queststg = "Q3" Then
q3a = vanswer
End If**
questno = questno + 1
Next
Using sqlconnection As New SqlConnection("Data Source=WillSQL\ict2;Initial Catalog=StudCourseEval;Integrated Security=SSPI")
commandtext = "Insert into tblQuestionnaire (Q1, Q2, Q3) values (@vq1a, @vq2a, @vq3a)"
Dim cmd As New SqlCommand(commandtext, sqlconnection)
sqlconnection.Open()
cmd.Parameters.AddWithValue("@vq1a", q1a)
cmd.Parameters.AddWithValue("@vq2a", q2a)
cmd.Parameters.AddWithValue("@vq3a", q3a)
cmd.ExecuteNonQuery()
sqlconnection.Close()
End Using
lblMSG.Text = sb.ToString()
End Sub
預先感謝您的幫助!
你需要比這更簡單的方法? –
爲什麼不使用數組或字典? – GSerg
這是什麼類型的應用程序?你能提供更多關於預期結果的信息嗎? – alybaba726