【问题标题】:How do I manage multiple FOR loops in Multiview Navigation Event Handler如何在多视图导航事件处理程序中管理多个 FOR 循环
【发布时间】:2017-12-20 01:05:26
【问题描述】:

在我试图解释我的问题时,请耐心等待。

我有多个 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

[已编辑]

【问题讨论】:

    标签: asp.net vb.net multiview


    【解决方案1】:

    你正面临这个问题,因为你是这样编码的......最简单的解决方法是创建一个 STRING VARIABLE 对象,每次点击时,程序都会检查 VARIABLE 是否有所需的值,如果值匹配然后它会移动到下一个表单。一个完整的例子看起来像这样:

     '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
    

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

    【讨论】:

    猜你喜欢
    • 2012-03-02
    • 1970-01-01
    • 2013-12-10
    • 2010-11-26
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-06-11
    相关资源
    最近更新 更多