ASP.net网站中的导航错误

时间:2009-10-02 15:01:53

标签: asp.net visual-studio-2008 web

我使用visual studio 2008开发了一个网站。它使用活动索引从一个页面导航到下一页面。

当用户单击下一个按钮时,它将递增索引。根据递增的值,导航了下一页

第一页有一个下拉值,其值为(A,B,C,D)

通常,网站以下列方式导航

Page1 - > page2 - > Page3 - > Page4等等..

但如果在Page1的下拉框中输入值“B”,则会导致以下流程。

第1页 - >第2页 - >第4页

但问题是......网站表现得很奇怪。它提供了以下流程..

Page1 - > page2 - > Page3 - > Page4而不是Page1 - > page2 - > Page4用于下拉选择中的特定值选择(值“B”)。

这个问题正在发生,当用户再次使用后退按钮进入最后一页时,使用“下一步”按钮进入最后一页。 (多次迭代导致此问题)

我需要的两件事:

  1. 为什么会这样?
  2. 如何防止此
  3. 我已经给出了以下代码:

    感谢您的帮助

    Protected Sub btn_view1_back_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_view1_back.Click, btn_view2_back.Click, btn_view3_back.Click
          Try
              Dim currentView As Int16
              currentView = mvRequestorForm.ActiveViewIndex
              If currentView = 3 And BSelected() = 1 Then
                  mvRequestorForm.ActiveViewIndex = (currentView - 2)
              Else
                  mvRequestorForm.ActiveViewIndex = (currentView - 1)
              End If
    
              If mvRequestorForm.ActiveViewIndex = 1 Then
                  If (ddl_view0_WULValue() = 0) Then
                      'C'
                      CSelected() = 1
                      strRoleType = "chkBx_C_workunit"
                  ElseIf (ddl_view0_WULValue() = 1) Then
                      'head office'
                      ASelected() = 1
                      strRoleType = "chkBx_A_workunit"
                  ElseIf (ddl_view0_WULValue() = 2) Then
                      'B'
                      BSelected() = 1
                      strRoleType = "chkBx_B_workunit"
                  ElseIf (ddl_view0_WULValue() = 3) Then
                      'B'
                      BSelected() = 1
                      strRoleType = "chkBx_B_workunit"
                  End If
              End If
    
          Catch ex As Exception
    
          End Try
    End Sub
    

    下面按下代码的代码:

    Protected Sub btn_View0_Next_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_View0_Next.Click, btn_View1_Next.Click, btn_view2_Next.Click
    
        Try
            Dim currentView As Int16
            currentView = mvRequestorForm.ActiveViewIndex
            If currentView = 1 And BSelected = 1 Then
                mvRequestorForm.ActiveViewIndex = (currentView + 2)
                rfv_view2_managersEmail.Enabled = True
                rev_view2_managersEmail.Enabled = True
            Else
                mvRequestorForm.ActiveViewIndex = (currentView + 1)
            End If
    
            If mvRequestorForm.ActiveViewIndex = 1 Then
                Sub_ActivateView1()
            End If
    
        Catch ex As Exception
    
        End Try
    End Sub
    

1 个答案:

答案 0 :(得分:5)

如果您发布了ASelected(),BSelected(),CSelected()和ddl_view0_WULValue(),那将会提供更多帮助。没有它,我无法确定为什么正在发生。我的猜测是因为Web应用程序中缺少状态。当您尝试在页面代码中设置变量时,对象仅在该单个回发期间保持不变(在这种情况下,单击按钮)。下次发生按钮点击时,页面的所有私有变量都将消失。

但是,下拉列表的状态将在回发之间保持,因此您可以直接查询下拉列表以找出已选择的内容。我将其更改为BSelected() = 1

,而不是ddl_view0.SelectedValue = "B"

我认为这就是你的aspx页面的样子。注意在ListItem上使用Text和Value属性。我们将在代码中使用Value属性来查找所选内容。

<asp:MultiView runat="server" ID="mvRequestorForm" ActiveViewIndex="0"
    <asp:View ID="View1" runat="server">
        1
        <asp:DropDownList ID="ddl_view0" runat="server">
            <asp:ListItem Text="Item A" Value="A"></asp:ListItem>
            <asp:ListItem Text="Item B" Value="B"></asp:ListItem>
            <asp:ListItem Text="Item C" Value="C"></asp:ListItem>
            <asp:ListItem Text="Item D" Value="D"></asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="btn_View0_Next" runat="server" Text="Next" />
    </asp:View>
    <asp:View ID="View2" runat="server">
        2
        <asp:Button ID="btn_View1_Next" runat="server" Text="Next" />
        <asp:Button ID="btn_view1_back" runat="server" Text="Back" />
    </asp:View>
    <asp:View ID="View3" runat="server">
        3
        <asp:Button ID="btn_view2_Next" runat="server" Text="Next" />
        <asp:Button ID="btn_view2_back" runat="server" Text="Back" />
    </asp:View>
    <asp:View ID="View4" runat="server">
        4
        <asp:Button ID="btn_view3_back" runat="server" Text="Back" />
    </asp:View>
</asp:MultiView>

我已经更改了你的代码以摆脱所有ASelected(),BSelected,CSelected和dll_view0_WULValue()调用,只需直接使用下拉列表。

Protected Sub btn_view1_back_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_view1_back.Click, btn_view2_back.Click, btn_view3_back.Click
    'Try'

    If mvRequestorForm.ActiveViewIndex = 3 And ddl_view0.SelectedValue = "B" Then 'CHANGED to use the SelectedValue property of the dropdownlist because it keeps state properly'
        mvRequestorForm.ActiveViewIndex -= 2 'CHANGED to a cleaner way to decrement by 2'
    Else
        mvRequestorForm.ActiveViewIndex -= 1 'CHANGED to a cleaner way to decrement by 1'
    End If

    If mvRequestorForm.ActiveViewIndex = 1 Then 'NOTE: This is now at least one less than when the method started. Is this what you really wanted?'

        If (ddl_view0.SelectedValue = "C") Then 'CHANGED to use the SelectedValue property of the dropdownlist because it keeps state properly'
            strRoleType = "chkBx_C_workunit"

        ElseIf (ddl_view0.SelectedValue = "A") Then 'CHANGED to use the SelectedValue property of the dropdownlist because it keeps state properly'
            strRoleType = "chkBx_A_workunit"

        ElseIf (ddl_view0.SelectedValue = "B") Then 'CHANGED to use the SelectedValue property of the dropdownlist because it keeps state properly'
            strRoleType = "chkBx_B_workunit"

        End If
        'NOTE: There seemed to be a duplicate case for "B" here, so I removed it'

    End If
    'Catch ex As Exception'
    'Commented out Try/Catch because empty catches hide bugs and make development harder'
    'End Try'
End Sub

Protected Sub btn_View0_Next_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_View0_Next.Click, btn_View1_Next.Click, btn_view2_Next.Click
    'Try'
    If mvRequestorForm.ActiveViewIndex = 1 And ddl_view0.SelectedValue = "B" Then
        mvRequestorForm.ActiveViewIndex += 2 'CHANGED to a cleaner way to increment by 2'
        rfv_view2_managersEmail.Enabled = True
        rev_view2_managersEmail.Enabled = True
    Else
        mvRequestorForm.ActiveViewIndex += 1 'CHANGED to a cleaner way to increment by 1'
    End If

    If mvRequestorForm.ActiveViewIndex = 1 Then 'NOTE: This is now at least one more than when the method started. Is this what you really wanted?'
        'Sub_ActivateView1()'
    End If
    'Catch ex As Exception'
    'Commented out Try/Catch because empty catches hide bugs and make development harder'
    'End Try'
End Sub

我不知道strRoleType的用途,但这可能无法正常工作。如果没有,请尝试在表单上删除HiddenField控件并使用它来存储值。