Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am proggramin an survey, in asp.net/ visual basis and I need to prevent that without filling a question (they are presentet with chechkboxes, radiobuttons and text boxes) to not move o to the next question!
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        For Each item As DataListItem In DataList1.Items
            If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
                Dim questionid As Integer
                Dim choiceid As Integer = 0
                Dim choicetext As String = ""
                questionid = CType(item.FindControl("Label3"), Label).Text
                Dim anstype As HiddenField = item.FindControl("HiddenField1")
                Select Case anstype.Value
                    Case "S"
                        Dim rbl As RadioButtonList = item.FindControl("RadioButtonList1")
                        If rbl.SelectedValue = "" Then
                            choiceid = 0
                        Else
                            choiceid = rbl.SelectedValue
                        End If
                        SaveAnswer(questionid, choiceid, "")
                    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
                                choiceid = cbl.Items(i).Value
                                SaveAnswer(questionid, choiceid, "")
                            End If
                        Next
                    Case "T"
                        Dim txt As TextBox = item.FindControl("TextBox1")
                        choicetext = txt.Text
                        SaveAnswer(questionid, 0, choicetext)
                End Select
            End If
        Next


I tried to put a mbox but it don't accept one!
Posted
Updated 8-May-13 8:13am
v2

1 solution

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
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900