locked
ViewState and PostBack problem with DropDownList RRS feed

  • Question

  • User1696470275 posted

    Hello,

    Sorry but this is driving me mad. I have two DropDown controls on my web form. I am storing their selected values in ViewState in the Page_PreLoad event as suggested by other sites. This works fine for one of the two but not for the other although the code is the same. Any help is much appreciated.

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    		Dim tmpList As List(Of String) = DataWeb.GetAreas
    
    		If IsNothing(tmpList) = True Then
    
    			Response.Redirect("notify.aspx?id=errorserver")
    			Exit Sub
    
    		End If
    
    		With cboBereich
    
    			.Items.Clear()
    			.Items.Add("")
    
    			For Each a In tmpList
    
    				.Items.Add(a)
    
    			Next
    
    		End With
    
    		With cboAnstellung
    
    			.Items.Clear()
    			.Items.Add("")
    
    			For Each c In DataWeb.lstContracts
    
    				.Items.Add(c)
    
    			Next
    
    		End With
    
    		If Me.ViewState("cboBereich") IsNot Nothing Then
    
    			cboBereich.SelectedIndex = CInt(Me.ViewState("cboBereich"))
    
    		Else
    
    			cboBereich.SelectedIndex = 0
    
    		End If
    
    		If Me.ViewState("cboAnstellung") IsNot Nothing Then
    
    			cboAnstellung.SelectedIndex = CInt(Me.ViewState("cboAnstellung"))
    
    		Else
    
    			cboAnstellung.SelectedIndex = 0
    
    		End If
    
    		AddHandler cmdSend.Click, AddressOf cmdSend_Click
    
    	End Sub
    	Protected Sub Page_PreLoad(sender As Object, e As EventArgs) Handles Me.PreLoad
    
    		Me.ViewState.Add("cboAnstellung", cboAnstellung.SelectedIndex)
    		Me.ViewState.Add("cboBereich", cboBereich.SelectedIndex)
    
    	End Sub



    Friday, November 3, 2017 10:07 AM

Answers

  • User475983607 posted

    christianpuhr

    I had that initially but changed it. Tried it again but does not work.

    No, the IsPostback convention does work. This pattern has been part of the ASP Web Form framework since the beginning.

    I suspect this line is not functioning as expected. Maybe DataWeb.GetAreas returns an empty list and not an unassigned object (Nothing) and the condition needs updating to handle this situation

    Dim tmpList As List(Of String) = DataWeb.GetAreas
    If IsNothing(tmpList) Or tmpList.Count = 0 Then
    
    	Response.Redirect("notify.aspx?id=errorserver")
    	Exit Sub
    
    End If

    Have you tried setting a breakpoint and single stepping through the code using the Visual Studio debugger?

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 3, 2017 1:47 PM

All replies

  • User475983607 posted

    ASP Web Forms using convention when initializing the page.

    If Not Page.IsPostBack Then
       ' bind dropdowns
    End If

    You're rebinding the dropdown on every request which resets the selected value.  Placing the binding logic inside the IF block above will allow the ASP state management framework to operate as designed and the selected values will be available throughout the page life cycle or until the dropdowns are bound to data sources.

    Friday, November 3, 2017 11:12 AM
  • User1696470275 posted

    Hello,

    Thanks! I had that initially but changed it. Tried it again but does not work. The first dropdown is emptied after each postback. I even tried removing the dropdown, recompiling and adding it again under a different name (cboArea).

    This is now the changed code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    		If IsPostBack = False Then
    
    			Dim tmpList As List(Of String) = DataWeb.GetAreas
    
    			If IsNothing(tmpList) = True Then
    
    				Response.Redirect("notify.aspx?id=errorserver")
    				Exit Sub
    
    			End If
    
    			With cboArea
    
    				.Items.Clear()
    				.Items.Add("")
    
    				For Each a In tmpList
    
    					.Items.Add(a)
    
    				Next
    
    			End With
    
    			With cboAnstellung
    
    				.Items.Clear()
    				.Items.Add("")
    
    				For Each c In DataWeb.lstContracts
    
    					.Items.Add(c)
    
    				Next
    
    			End With
    
    		End If
    
    		'AddHandler cmdSend.Click, AddressOf cmdSend_Click
    
    	End Sub

    And parts of the HTML:

            <div class="headerlabel">In welchem Bereich arbeiten Sie?</div>
            <asp:DropDownList ID="cboArea" runat="server"></asp:DropDownList>
            <div class="headerlabel">Welches Anstellungsverhältnis haben Sie?</div>
            <asp:DropDownList ID="cboAnstellung" runat="server"></asp:DropDownList>

    Friday, November 3, 2017 1:11 PM
  • User475983607 posted

    christianpuhr

    I had that initially but changed it. Tried it again but does not work.

    No, the IsPostback convention does work. This pattern has been part of the ASP Web Form framework since the beginning.

    I suspect this line is not functioning as expected. Maybe DataWeb.GetAreas returns an empty list and not an unassigned object (Nothing) and the condition needs updating to handle this situation

    Dim tmpList As List(Of String) = DataWeb.GetAreas
    If IsNothing(tmpList) Or tmpList.Count = 0 Then
    
    	Response.Redirect("notify.aspx?id=errorserver")
    	Exit Sub
    
    End If

    Have you tried setting a breakpoint and single stepping through the code using the Visual Studio debugger?

    https://msdn.microsoft.com/en-us/library/y740d9d3.aspx

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 3, 2017 1:47 PM
  • User1696470275 posted
    Hi,

    With „it did not work“ I meant that I had the same problem. The function works because the dropdown list is populated correctly. It is strange that it only shows the problem with the one dropdown and not with the other. Furthermore, it is only in IE and not in Chrome. I will try the breakpoint, though. Thanks!

    Christian
    Friday, November 3, 2017 6:22 PM
  • User347430248 posted

    Hi christianpuh...,

    I agree with meghbard about the checking the values of "DataWeb.GetAreas".

    you had mentioned that the issue is only in IE and Chrome.

    so does it work properly in other browser?

    did you try to check by changing the source of the value?

    if not you can try to have a check.

    how did you fetch the values in "DataWeb.GetAreas". if possible then you can also post the code about it.

    it will help us to understand the issue in better way.

    Regards

    Deepak

    Monday, November 6, 2017 6:29 AM
  • User1696470275 posted

    Hello!

    After some checks I believe that the data binding was the problem, after all. I had not time to dig very deep but when I populated the dropdown with data from an SQL database the problem occurred. When I populated it with an array from code behind it did not. So something was wrong with the data transfer. Will dig deeper when there is time.

    Thank you for the support!

    Best regards,

    Christian

    Wednesday, November 15, 2017 8:19 AM