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
    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
    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?

Clifton BardwellProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

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.

Use the selectedvalue property rather than text.

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 ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
You can try:


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
Kamal KhaleefaInformation Security SpecialistCommented:
try to use YourDropDowlListID.SelectedValue or YourDropDowlListID.SelectedItem.Text
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

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.  :-)
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
            If g_sCurrentState.Trim <> "" Then
                ddlStates.Text = g_sCurrentState
            End If
            g_sCurrentStateID = ViewState("StateID")
            g_sCurrentState = ViewState("State")
            If g_sCurrentState <> "" Then
                ddlStates.Text = g_sCurrentState
            End If
        End If

Open in new window

Ryan ChongBusiness Systems Analyst , ex-Senior Application EngineerCommented:
in Page_Load, are you trying to retrieve the ddlStates's selected value or set its value?
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).
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

From novice to tech pro — start learning today.