get the current selected value in a dropdownlist ASP.net

I have a dropdownlistbox that when I try to read the value of the currently selected value in the list I get the first value NOT what was selected.

this is the syntax I am using.
dim strS as string
strs = ddlSaleContact.SelectedValue
troyvwAsked:
Who is Participating?

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

x
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.

zadeveloperCommented:
try:
Dim strS As String
        strS = ddlSaleContact.SelectedItem.Value

Open in new window

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
waltersnowslinarnoldCommented:
try the following code sample..,
Dim strS As String
strS = ddlSaleContact.SelectedItem.Text;

Open in new window

0
zadeveloperCommented:
Just keep in mind when you bind your dropdownlist
if you set the displaymember = "DisplayName" and ValueMember = "ID"
strS = ddlSaleContact.SelectedItem.Value will return the ID of the property of the object and strS = ddlSaleContact.SelectedItem.Text will return the  DisplayName property of the object. Usually it is best practice to store your working field in the Value property.
Just something to keep in mind.
0
Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

guru_samiCommented:
--->  I get the first value NOT what was selected.

That means your DDL is being bound on every postback and so you are losing your selectedValue.
So post your code where you are databinding the DDL.
If your pageLoad you should check if it is not postback....then only databind.

if(!Page.IsPostBack)
{ //bind your ddl
}
0
zadeveloperCommented:
I think guru_sami is correct.
0
waltersnowslinarnoldCommented:
exactly, just do as below..,

If(!IsPostBack)
{
 Dim strS As String
 strS = ddlSaleContact.SelectedItem.Text;
}
0
zadeveloperCommented:
nope ... actually
exactly, just do as below..,

on page load:

If(!IsPostBack)
{
load ddlSaleContact with data
}
 -----------
then you can use ddlSaleContact.SelectedValue on the events fired (ie: button, index changed etc...)
0
waltersnowslinarnoldCommented:
Oops! @zadeveloper is correct. You cannot do a s I did. Infact dropdownlist has to be loaded with suitable data at Page_Load, and catch in any event the selecteditem text.
0
raaziqCommented:
the issue is ..

in page load u need to check IsPostback before binding the dropdownlist...so that is doesnt get bound in every postback.

then u will get the selected item value properly.
0
apexpertCommented:
i think there are 2 problems--
1. DDL is Bind on each postback or
2. DDL has same multiple values check view source of ur data which is bind in DDL.
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
.NET Programming

From novice to tech pro — start learning today.