DropDownList problems

joe_pool_is

Contributor
Joined
Jan 18, 2004
Messages
507
Location
Longview, TX [USA]
I have put together a small form with 3 different DropDownLists. The items to each DropDownList is loaded programatically during the onLoad event.

One is ddlIDType (where the customer enters their ID Type),
One is ddlState (where the customer enters their home state), and
One is ddlYear (where the customer enters the year of their birth).

In my code, I gather the information as follows:
Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim MyList As New DropListData ' A class I have with all the states and years
ddlState.DataSource = MyList.StateList
ddlState.DataBind()
ddlYear.DataSource = MyList.YearList
ddlYear.DataBind()
With ddlIDType.Items
	.Add("Driver's License")
	.Add("Passport")
	.Add("Alien Registration")
	.Add("Govt. Employee")
	.Add("Military")
End With 
End Sub
 
Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim strCustomerInfo as String = ""
 
strCustomerInfo &= ddlIDType.SelectedItem.Text
strCustomerInfo &= ddlState.SelectedItem.Text
strCustomerInfo &= ddlYear.SelectedItem.Text
End Sub
The IDType field works the way it should, but State and Year do not. This leads me to believe that somehow my databinding is not working the way I want it to. The data shows up in each of the DropDownList when I run the webpage. When I click the btnSubmit button, State and Year consistantly return the topmost value, no matter which value was selected.

Could someone explain what is going on, and how I could correct this?
 
Are you rebinding the controls in the page_load every time? If so that will reset the list selection. You need to check Page.IsPostBack and only bind when it's false.
 
Thanks! I never knew the Page keyword was there.

I have another question that you may be able to help me with: On my form, when I click SUBMIT, the page reloads some of the time (but not all of the time). I put a breakpoint on the "Sub btnSUBMIT_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handels btnSUBMIT.Click", but even when I click the SUMBIT button, the debugger never goes to this sub - It goes straight to Page_Load.

What is this? Could you offer any solution(s) here?
 
The internet is inherantly stateless, once the server has sent a page to the browser it no longer cares about the browser and the page is discarded. When you submit the page back to the server it has to recreate the page from fresh - including runnning all the page_init, page_load events before it will execute control events.
 
FYI

You also may want to look into using a StringBuilder and the Append method. That way you aren't creating a new string each time you concatenate with the &.
 
Back
Top