Link to home
Start Free TrialLog in
Avatar of Clifton Bardwell
Clifton BardwellFlag for United States of America

asked on

Getting Text Value of a DropDownList

This question is about ASP.Net/VB.Net with the VS.Net 2008 platform.

I have a DropDownList on my page.  I filled it during the page load.  I also have a button on the form.  The code to the button looks something like this:
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        MsgBox(ddlStates.Text)
    End Sub

Open in new window

The issue seems to be that, no matter what the user selects, it always returns whatever was set as the default when the DropDownList was loaded.

The DropDownList doesn't seem to react to SelectedIndexChanged either.
    Private Sub ddlStates_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlStates.SelectedIndexChanged
        MsgBox(ddlStates.Text)
    End Sub

Open in new window

I'll admit it's been a while since I've done any programming, but I'm thinking it should work.  So, what am I doing wrong?

TIA
ASKER CERTIFIED SOLUTION
Avatar of ChloesDad
ChloesDad
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Clifton Bardwell

ASKER

I have been out for a few days and, so, have been unable to address this.  I will do so in the next couple of days.

Just letting y'all know I haven't abandoned this question.  :-)
Sorry for the delay, but I had to go through some (unrelated) training.

Neither SelectedValue nor SelectedItem.Text worked.
As far as using the Page_Load event, this is (basically) what I have:
        loadDefaults()  '<-- Sets g_sCurrentState to "North Carolina", among other presets
        If Not IsPostBack Then
            loadStateList()
            If g_sCurrentState.Trim <> "" Then
                ddlStates.Text = g_sCurrentState
            End If
        Else
            g_sCurrentStateID = ViewState("StateID")
            g_sCurrentState = ViewState("State")
            loadStateList()
            If g_sCurrentState <> "" Then
                ddlStates.Text = g_sCurrentState
            End If
        End If

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
It depends on whether its a postback.

On a fresh page, the value in "g_sCurrentState" is a system default value (basically a starter value).

On a page that has been posted back, "g_sCurrentState" is set to a ViewState value which should have been set during the ddlStates.SelectedIndexChanged() event.

I just checked to make sure that is what my code was supposed to do.  During testing, however, I find that the SelectedIndexChanged() event doesn't fire when you select a different State than the current one.

I also have a "Save" button, which currently does nothing more than display a messagebox with the ddlStates' value.  It appears that, when I click on this button, the SelectedIndexChanged() event is fired (but still retains it's original value).