如何在多视图导航事件处理程序中管理多个FOR循环

问题描述 投票:0回答:1

当我试着解释我的问题时,请跟我一点。

我有多个GridView控件,每个控件都有自己的复选框和文本框。

用户必须选中复选框或在所有文本框中输入数据。

如果未选中复选框并且特定gridview控件中的文本框为空,则当用户单击多视图控件的NEXT按钮时,将引发错误。

以下代码显示了NEXT导航按钮的按钮单击事件中的两个FOR循环。

第一个Gridview1 FOR循环效果很好。如果未选中复选框且文本框为空,则会显示警告消息,用户无法导航到下一页。

但是,如果选中复选框或文本框中填充了数据,则用户可以成功导航到下一页。

问题在于grvspouse gridview控件的第二个FOR循环。

如果未选中复选框且文本框为空,则会显示警告框,其中包含用户必须复选框或将数据输入文本框的消息。这可以。但是,问题是用户仍然被带到下一页。

有没有办法在BTN_NEXT导航事件处理程序中处理多个FOR循环?

Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)

    'If the message failed at some point, let the user know
    For Each row As GridViewRow In Gridview1.Rows
        Dim namesource As TextBox = TryCast(row.FindControl("txtsourcename"), TextBox)
        Dim nmesource As String = namesource.Text
        Dim addresssource As TextBox = TryCast(row.FindControl("txtsourceaddress"), TextBox)
        Dim addrsource As String = addresssource.Text
        Dim incomesource As TextBox = TryCast(row.FindControl("txtsourceincome"), TextBox)
        Dim incmsource As String = incomesource.Text
        Dim ckb As CheckBox = TryCast(row.FindControl("grid1Details"), CheckBox)
        Dim checkb As Boolean = ckb.Checked
        If checkb = False AndAlso nmesource = "" AndAlso addrsource = "" AndAlso incmsource = "" Then
            ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
        Else
            myMultiView.ActiveViewIndex += 1
            lblResult.Visible = True
        End If
    Next

    For Each row As GridViewRow In grvspouse.Rows
        Dim namespouse As TextBox = TryCast(row.FindControl("txtspousename"), TextBox)
        Dim nmespouse As String = namespouse.Text
        Dim addressspouse As TextBox = TryCast(row.FindControl("txtspouseaddress"), TextBox)
        Dim addrspouse As String = addressspouse.Text
        Dim incomespouse As TextBox = TryCast(row.FindControl("txtspouseincome"), TextBox)
        Dim incmspouse As String = incomespouse.Text
        Dim ckb2 As CheckBox = TryCast(row.FindControl("spouseDetails"), CheckBox)
        Dim checkc As Boolean = ckb2.Checked
        If checkc = False AndAlso nmespouse = "" AndAlso addrspouse = "" AndAlso incmspouse = "" Then
            ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
        Else
            myMultiView.ActiveViewIndex += 1
            lblResult.Visible = True
        End If
    Next

End Sub

将帖子

enter image description here

asp.net vb.net multiview
1个回答
0
投票

您正面临这个问题,因为您以这种方式编码....最简单的解决方法是创建一个STRING VARIABLE对象,每次单击,程序将检查VARIABLEfor所需的值,如果值匹配,那么它将移动到下一个表单。完整的示例如下所示:

 'Create a VARIABLE named HITCOUNT and keep the value empty at first.
  Public class MyProject
  Dim HITCOUNT as string = ""


 If HITCOUNT = "" Then
 For Each row As GridViewRow In Gridview1.Rows
Dim namesource As TextBox = TryCast(row.FindControl("txtsourcename"), TextBox)
Dim nmesource As String = namesource.Text
Dim addresssource As TextBox = TryCast(row.FindControl("txtsourceaddress"), TextBox)
Dim addrsource As String = addresssource.Text
Dim incomesource As TextBox = TryCast(row.FindControl("txtsourceincome"), TextBox)
Dim incmsource As String = incomesource.Text
Dim ckb As CheckBox = TryCast(row.FindControl("grid1Details"), CheckBox)
Dim checkb As Boolean = ckb.Checked
If checkb = False AndAlso nmesource = "" AndAlso addrsource = "" AndAlso incmsource = "" Then
    ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
Else
    myMultiView.ActiveViewIndex += 1
    lblResult.Visible = True
End If
Next
HITCOUNT = "1" 'adding an integer value of 1(you can add anything)
End if

If HITCOUNT = "1" then
For Each row As GridViewRow In grvspouse.Rows
Dim namespouse As TextBox = TryCast(row.FindControl("txtspousename"), TextBox)
Dim nmespouse As String = namespouse.Text
Dim addressspouse As TextBox = TryCast(row.FindControl("txtspouseaddress"), TextBox)
Dim addrspouse As String = addressspouse.Text
Dim incomespouse As TextBox = TryCast(row.FindControl("txtspouseincome"), TextBox)
Dim incmspouse As String = incomespouse.Text
Dim ckb2 As CheckBox = TryCast(row.FindControl("spouseDetails"), CheckBox)
Dim checkc As Boolean = ckb2.Checked
If checkc = False AndAlso nmespouse = "" AndAlso addrspouse = "" AndAlso incmspouse = "" Then
    ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
Else
    myMultiView.ActiveViewIndex += 1
    lblResult.Visible = True
End If
Next
HITCOUNT="2"
End if

  'and move on and on

这只是一种方式,也许是最快的方式。还有很多其他方法可以做到。如果你不想使用这个,只需评论,我会发布另一个解决方案

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.