?
Solved

get the current selected value in a dropdownlist ASP.net

Posted on 2010-01-05
11
Medium Priority
?
555 Views
Last Modified: 2013-11-08
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
0
Comment
Question by:troyvw
10 Comments
 
LVL 13

Accepted Solution

by:
zadeveloper earned 800 total points
ID: 26180961
try:
Dim strS As String
        strS = ddlSaleContact.SelectedItem.Value

Open in new window

0
 
LVL 7

Assisted Solution

by:waltersnowslinarnold
waltersnowslinarnold earned 600 total points
ID: 26181518
try the following code sample..,
Dim strS As String
strS = ddlSaleContact.SelectedItem.Text;

Open in new window

0
 
LVL 13

Assisted Solution

by:zadeveloper
zadeveloper earned 800 total points
ID: 26181554
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
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 41

Assisted Solution

by:guru_sami
guru_sami earned 200 total points
ID: 26181887
--->  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
 
LVL 13

Assisted Solution

by:zadeveloper
zadeveloper earned 800 total points
ID: 26182008
I think guru_sami is correct.
0
 
LVL 7

Assisted Solution

by:waltersnowslinarnold
waltersnowslinarnold earned 600 total points
ID: 26182042
exactly, just do as below..,

If(!IsPostBack)
{
 Dim strS As String
 strS = ddlSaleContact.SelectedItem.Text;
}
0
 
LVL 13

Assisted Solution

by:zadeveloper
zadeveloper earned 800 total points
ID: 26182085
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
 
LVL 7

Assisted Solution

by:waltersnowslinarnold
waltersnowslinarnold earned 600 total points
ID: 26182142
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
 
LVL 3

Assisted Solution

by:raaziq
raaziq earned 200 total points
ID: 26187471
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
 
LVL 4

Assisted Solution

by:apexpert
apexpert earned 200 total points
ID: 26188309
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question