views:

200

answers:

1

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
+5  A: 

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.

Greg