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
Clifton BardwellProgrammerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ChloesDadCommented:
Use the selectedvalue property rather than text.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ryan ChongCommented:
You can try:

ddlStates.SelectedItem.Text

alternatively, you can place your scripts into Page_Load event and check the IsPostBack value, like:

Sub Page_Load
    If IsPostBack
         '.....
    End If
End Sub

and make sure the AutoPostBack is set to "True" for your DropDownList control
0
Kamal KhaleefaInformation Security SpecialistCommented:
try to use YourDropDowlListID.SelectedValue or YourDropDowlListID.SelectedItem.Text
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Clifton BardwellProgrammerAuthor Commented:
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.  :-)
0
Clifton BardwellProgrammerAuthor Commented:
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

0
Ryan ChongCommented:
in Page_Load, are you trying to retrieve the ddlStates's selected value or set its value?
0
Clifton BardwellProgrammerAuthor Commented:
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).
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.