I use validation flags, in which on submit, I check each element for a value, and if the value fails, I change the flag to false, and if the flag is still true, I start the postback, if false, I exit sub.
Dim vFlag as boolean = true
Dim raw_Anwser1 As String = txt_Anwser1.text.trim
if (raw_Anwser1.length < 2) then
'Set the textbox to the error state, with colors or something
vFlag = false
Else
'reset the textbox to normal
End if
if (vFlag = False) then
'Validation Failed, tell the user, show an arrow, or paint the control red
Exit Sub
else
'execute the forward submit code
End if
But, after looking at your code for awhile, I think I spotted some errors with it.
I'm not a fan of findcontrol, and I don't use the hiddenfield object, I use css display: none instead on a text box. So I'm not sure if your finding your objects or not.
I'm not really grooving in the way it's designed in the first place.
For Each item As DataListItem In DataList1.Items
If (item.ItemType = ListItemType.Item) Or _
(item.ItemType = ListItemType.AlternatingItem) Then
Dim choice_ID As Integer = 0
Dim question_ID As String = CType(item.FindControl("Label3"), Label).Text
Dim anstype As TextBox = item.FindControl("HiddenField1").text
Select Case anstype.Text.ToUpper
Case "S"
Dim rbl As RadioButtonList = item.FindControl("RadioButtonList1")
If Not (rbl.SelectedValue = "") Then
choice_ID = rbl.SelectedValue
End If
SaveAnswer(question_ID, choice_ID, "")
Case "M"
Dim cbl As CheckBoxList = item.FindControl("CheckBoxList1")
For i As Integer = 0 To cbl.Items.Count - 1
If cbl.Items(i).Selected Then
choice_ID = cbl.Items(i).Value
SaveAnswer(question_ID, choice_ID, "")
End If
Next
Case "T"
Dim txt As TextBox = item.FindControl("TextBox1")
Dim choice_Text As String = txt.Text
SaveAnswer(question_ID, 0, choice_Text)
End Select
End If
Next
End Sub