ASP.net 网站中的导航错误

发布于 2024-08-06 16:28:51 字数 2690 浏览 1 评论 0原文

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

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

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

通常,网站按以下方式导航:

Page1-->page2-->Page1-->page2-->Page1-->page2--> Page3-->Page4 依此类推。

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

Page1-->page2-->Page4

但问题是......网站表现得很奇怪。它给出以下流程..

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

当用户再次转到最后一页时,使用后退按钮回到前面并使用“下一页”按钮转到最后一页,就会发生此问题。 (多次迭代导致此问题)

我在这里需要两件事:

  1. 为什么会发生?
  2. 如何防止这种情况

我已经给出了下面的代码:

感谢您提前的帮助,

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

I have developed a website using visual studio 2008. Which uses active index to navigate from one page to the next page.

It will increment the index when the user clicks the next button. According to the incremented value , next pages has been navigated

There is a drop down in the first page with values (A,B,C,D)

Normally, Website navigate in the the following way

Page1-->page2-->Page3-->Page4 and so on..

But if value "B" in a drop down box in Page1 which result in the following flow.

Page1-->page2-->Page4

But the problem is ... Website is acting strange. It gives following flow..

Page1-->page2-->Page3-->Page4 instead of Page1-->page2-->Page4 for that particular value selection (value "B") in drop down selection.

This issue is happening, when user goes to the last page again come to the front using back button and go the last page using “next “button. (Multiple iterations causing this issue)

Two things i need here :

  1. Why it is occurring?
  2. How to prevent this

I have given the code below :

Thanks for the help in advance

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

Code for Next button below :

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

静赏你的温柔 2024-08-13 16:28:52

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

但是,下拉列表的状态将在回发之间保持,因此您可以直接查询下拉列表以了解已选择的内容。我将其更改为 ddl_view0.SelectedValue = "B",而不是 BSelected() = 1

我认为这就是您的 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 控件并使用它来存储值。

If you posted ASelected(), BSelected(), CSelected() and ddl_view0_WULValue() that would have helped more. Without that I can't really determine why it's happening. My guess is because of the lack of state in a web application. When you try to set variables in a page's code behind, objects only stay around during that single postback (in this case, a button click). The next time a button click happens, all of that data your page's private variables will be gone.

However, the state of the drop down list will be maintained between postbacks, so you can query the drop down list directly to find out what's been selected. Instead of BSelected() = 1, I've changed it to ddl_view0.SelectedValue = "B"

I assumed that this is what your aspx page looked like. Notice the use of Text and Value properties on the ListItem. We'll use the Value property in the code to find what is selected.

<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>

I've changed your code to get rid of all of the ASelected(), BSelected, CSelected, and dll_view0_WULValue() calls and just use the drop down list directly.

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

I don't know what strRoleType is for, but that probably won't work correctly either. If it doesn't, try dropping a HiddenField control on your form and use that to store the value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文