0
我正在嘗試掃描我擁有的某些True/False RadioButtonLists。它們在一個QuestionsList中。我試圖給每個項目添加一條消息,說「正確」或「不正確」,但是當我這樣做時,我得到「集合被修改;枚舉操作可能不會執行。」使用For For修改集合的副本仍然給出錯誤
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
If (IsPostBack) Then
Dim SubmittedQuestionsList As New HtmlGenericControl("ol")
SubmittedQuestionsList = QuestionsList
For Each QuestionItem In SubmittedQuestionsList.Controls
If TypeOf QuestionItem Is HtmlGenericControl Then
For Each question In QuestionItem.Controls
If TypeOf question Is RadioButtonList Then
Dim QuestionExplanation As New Panel
If (question.SelectedValue = "true") Then
QuestionExplanation.CssClass = "succcess-response"
QuestionExplanation.Controls.Add(New LiteralControl("Correct!"))
Else
QuestionExplanation.CssClass = "error-response"
QuestionExplanation.Controls.Add(New LiteralControl("Incorrect:"))
End If
'If I uncomment the line below, error goes away
QuestionsList.Controls.Add(QuestionExplanation)
Debug.WriteLine(question.SelectedValue)
End If
Next
End If
Next
End If
End Sub
什麼是最好的方式來循環它(或它的副本),並添加一條消息,因爲我去?
SubmittedQuestionsList只是另一種參照同QuestionsList。可以使用ProblemsList的一個克隆,使用For循環而不是For Each,或者追蹤哪些項目需要修改並在單獨的循環中進行修改。 –
我以爲SubmittedQuestionsList是一個ProblemsList的克隆...你知道如何正確地克隆它而不是僅僅引用它嗎? –
我認爲應該更高效地將控件添加到中間數組,然後在循環結束時使用對Controls.AddRange()的單個調用。這將會產生所需的副作用,即不會更改循環中的集合。 –