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?
 
zadeveloperConnect With a Mentor Commented:
try:
Dim strS As String
        strS = ddlSaleContact.SelectedItem.Value

Open in new window

0
 
waltersnowslinarnoldConnect With a Mentor Commented:
try the following code sample..,
Dim strS As String
strS = ddlSaleContact.SelectedItem.Text;

Open in new window

0
 
zadeveloperConnect With a Mentor Commented:
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
guru_samiConnect With a Mentor Commented:
--->  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
 
zadeveloperConnect With a Mentor Commented:
I think guru_sami is correct.
0
 
waltersnowslinarnoldConnect With a Mentor Commented:
exactly, just do as below..,

If(!IsPostBack)
{
 Dim strS As String
 strS = ddlSaleContact.SelectedItem.Text;
}
0
 
zadeveloperConnect With a Mentor Commented:
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
 
waltersnowslinarnoldConnect With a Mentor Commented:
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
 
raaziqConnect With a Mentor Commented:
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
 
apexpertConnect With a Mentor Commented:
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
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.

All Courses

From novice to tech pro — start learning today.