AutoPostBack - 2 Work, 1 Doesn't

My third dropdownlist is set to AutoPostBack, so I can display the correct race distance. I THINK it is written the same way the “Category” and “Event” choices are shown above. Category and Event work perfectly, but when I choose an event distance (ie 5K, or 10K), it changes back to the first one listed in the table (ie 5K).

Can you see what I’m doing wrong? If I turn AutoPostBack to “False”, then I don’t get the correct charge info from the database, so it seems I need to keep it as “true”.


Sub Page_Load()

            If Not IsPostBack Then
                BindData()
				
            End If
End Sub

Sub BindData()
Try													
                objConn.Open()
                    objCmd = New OleDbCommand("SELECT * FROM tblEvents WHERE EDate>Now() AND EType=3 OR EType=5 ORDER BY EDate Desc", objConn)
                    objRdr = objCmd.ExecuteReader()
                    ddlEvent.DataSource = objRdr
                    ddlEvent.DataValueField = "EID"
                    ddlEvent.DataTextField = "EName"
                    ddlEvent.DataBind()
                        While objRdr.Read()

                            lblEventMesg.Text = "<b>" & ddlEvent.SelectedItem.Text & objRdr.Item("EDate") & "</b>"
							
											
							
							
                        End While
				
				Catch
				
			 lblNoEvents.Text = "Sorry, there are no upcoming events for which to register"
					 pnlTMem1.visible = false
					 btnCalculate.visible = false
				End Try

                    objRdr.Close()
                objConn.Close()


objConn.Open()

                    objCmd = New OleDbCommand("SELECT * FROM tblCategory WHERE CategoryDuathlon='Y' ORDER BY CategoryCount ASC", objConn)
                    objRdr = objCmd.ExecuteReader()
                    ddlCategory.DataSource = objRdr
                    ddlCategory.DataValueField = "CategoryID"
                    ddlCategory.DataTextField = "Category"

                    ddlCategory.DataBind()
                    lblCatMesg.Text = ddlCategory.SelectedItem.Text
                objRdr.Close()
                objConn.Close()

End Sub

 Sub Category(s As Object, e As EventArgs)
    '############ This displays registration forms for the selected number of teammates ######
                    Dim strCat
                    strCat = ddlCategory.SelectedItem.Text
                    objConn.Open()
                    objCmd = New OleDbCommand("SELECT * FROM tblCategory WHERE Category= '" & strCat & "'", objConn)
                    objRdr = objCmd.ExecuteReader()
                            lblCatMesg.Text = ddlCategory.SelectedItem.Text
                        While objRdr.Read()
                            pnlTMem1.Visible = True
                            pnlTMem2.Visible = False
							
                            pnlPayment.Visible = False
                        If objRdr.Item("CategoryCount") = "2" Then
                            pnlTMem1.Visible = True
                            pnlTMem2.Visible = True
                            pnlPayment.Visible = False
                        
                        End If
                lblCount.Text = objRdr.Item("CategoryCount")
            End While

                objRdr.Close()
                objConn.Close()
    End Sub

    Sub Events(s As Object, e As EventArgs)
                Try	
				    Dim strEvent
                    strEvent = ddlEvent.SelectedItem.Text
				
                    objConn.Open()

                    objCmd = New OleDbCommand("SELECT * FROM tblEvents WHERE EName= '" & strEvent & "'", objConn)
                    objRdr = objCmd.ExecuteReader()
                        While objRdr.Read()
                            lblEventMesg.Text = ddlEvent.SelectedItem.Text

                            lblEventDate.Text = objRdr.Item("EDate")
                        End While
						
						
                objRdr.Close()
                objConn.Close()
				
				                
					
            	
				
				
				Catch
				lblNoEvents.Text = "Sorry, there are no upcoming events for which to register"
				pnlTMem1.visible = false
				btnCalculate.visible = false
				End Try
	Distance2()

    End Sub
	
		Sub Distance2()
	
			objConn.Open()		
	        objCmd = New OleDbCommand("SELECT * FROM tblEventDistances WHERE tblEventDistances.EID=@EID", objConn)
             objCmd.Parameters.Add("@EID", ddlEvent.SelectedItem.Value)
			
                objRdr = objCmd.ExecuteReader()
				
			
                ddlDistance.DataSource = objRdr
                ddlDistance.DataValueField = "tblEDistanceID"
                ddlDistance.DataTextField = "Distance"
				
                ddlDistance.DataBind()
				
			
						
                objRdr.Close()
                objConn.Close()
				
			
	End Sub
	
	
	Sub Distance(s As Object, e As EventArgs)
	 	Dim strDistanceSelected
      	strDistanceSelected = ddlDistance.SelectedItem.Text
				
       objConn.Open()

        objCmd = New OleDbCommand("SELECT * FROM tblEventDistances WHERE Distance=@Distance", objConn)
        objCmd.Parameters.Add("@Distance", ddlDistance.SelectedItem.Text)
		
		objRdr = objCmd.ExecuteReader()
         
		While objRdr.Read()
           lblDistanceID.Text = objRdr.Item("Distance")
        End While
		objRdr.Close()
         objConn.Close()
	End Sub

-----------
		<strong>SECTION ONE: </strong><BR /><br />
                        <strong>Your Competition Category:</strong>&nbsp;&nbsp;
                        <asp:DropDownList id="ddlCategory" runat="server" OnLoad="Category" AutoPostBack="true" OnSelectedIndexChanged="Category"></asp:DropDownList>
                       
					  		
						
                     <br> <br> <b>You have selected:</b>&nbsp;&nbsp;  <asp:Label id="lblCatMesg" forecolor="red" font-bold="true" runat="server"></asp:Label>&nbsp;&nbsp; # of Racers: <asp:Label id="lblCount" forecolor="red" font-bold="true" runat="server"></asp:Label>
                        
						
                       
					    <br />
						<hr>
						
                        <br />
						 <strong>SECTION TWO:</strong><BR /><br />
                        <strong>Event:</strong> &nbsp;&nbsp;
                        <asp:DropDownList id="ddlEvent" runat="server" OnLoad="Events" AutoPostBack="true" OnSelectedIndexChanged="Events"></asp:DropDownList>
                        <br>
						
                       <br> <b>You have selected:</b> &nbsp;&nbsp; <asp:Label id="lblEventMesg" runat="server" forecolor="red" font-bold="true"></asp:Label>&nbsp; &nbsp; <asp:Label id="lblEventDate" font-bold="true" forecolor="red" runat="server"></asp:Label>
                        <br />
                        <br />
                    </p>
					<hr>
					 <br />
	 <strong>SECTION THREE:</strong><BR /><br />				 
	      <strong>Event Distance:</strong> &nbsp;&nbsp;
           <asp:DropDownList id="ddlDistance" runat="server" AutoPostBack="true" OnSelectedIndexChanged="Distance"></asp:DropDownList>
                        <br>
						
			<b>You have selected:</b> &nbsp;&nbsp; <asp:Label ID="lblDistanceID" runat="server" forecolor="red" font-bold="true"></asp:Label>

Okay, I know WHY it is resetting my Distance DropDownList to the first choice in my database, but I don’t know HOW to correct it, without messing up the other function.

My Distance DDL is resetting, because on AutoPostBack, it is calling my Sub Event, which in turn calls the “Distance2()”, which populates my Distance DDL. (My Distance DDL is dependent upon which Event is chosen).

I have tried “If NOT ISPOSTBACK” to try to stop resetting the Distance DDL, but then it only shows the Distance choices for whatever Event is loaded on the initial page load.

Any ideas on how to correct this? Thank you.

Well, when I tried to include the BindData in the same Sub - End Sub that the “Event” and “Category” dropdownlists were in, the “Distance” dropdownlist only shows the distance choice for whatever event is shown when the page first loads.

I’ve been trying tons of stuff to try to make it work. I’m not understanding why it will not just work like the first two.

I added this to the top of my page:

<%@ Page Language="vb" EnableViewState="true" %>

Now, I just changed my Distance to the following code, but it doesn’t work correctly either. The AutoPostBack=“true” causes it to default back to the “5 K” option, instead of staying on the “10 k” that I select.

	Sub Distance(s As Object, e As EventArgs)
				
       objConn.Open()

        objCmd = New OleDbCommand("SELECT * FROM tblEventDistances WHERE EID=@EID", objConn)
        'objCmd.Parameters.Add("@Distance", ddlDistance.SelectedItem.Value)
		objCmd.Parameters.Add("@EID", ddlEvent.SelectedItem.Value)
		
		objRdr = objCmd.ExecuteReader()
                ddlDistance.DataSource = objRdr
                ddlDistance.DataValueField = "tblEDistanceID"
                ddlDistance.DataTextField = "Distance"
			    ddlDistance.DataBind()
				 
		While objRdr.Read()
	
		
           lblDistanceID.Text = objRdr.Item("Distance")
        End While
		objRdr.Close()
         objConn.Close()
		 
		 
	End Sub

Why are you binding the dropdownliast on Distanace again? Isnt it already been bound on first page load? If not thats what it should be (provided you have enableviewstate=true).