DropDownList always returning the first item

S

Siamak Zahedi

Hi,
weird problem I'm having cant seem to figure out what is going on.

I have a dropdownlist that gets populated from db and a button that causes
a post back
when I assign the value of the dropdownlist to a string var it always
returns the first item regardless of what was selected

code is simple

in the button click event

string name = ddlName.SelectedItem.ToString();

I'm learning asp.net and this is my first project so excuse the newbie
question .
 
G

Guest

Did you try “name = ddlName.SelectedItem.Valueâ€.
ToString() is probably not the selected ListItem value.

Shaw
 
S

Siamak Zahedi

yes things ive tryed so far:

string name = ddlSelectedItem.Value;
string name = ddlSelectedItem.Text;

the first one returns the SelectedValue of the first item in the list
and the Text returns the same as the ToString i had before

im starting to think its maybe because i have it placed in a panel which is
also inside a Main panel
page has a main panel that is visible and a second panel that is not visible
if the user clicks the post back button on the main panel and all the
procedures run with out error the second panels is set to visible and page
is rendered.

on the second panel i have a databound dropdown (one that is causeing the
problem) a html File input set to run on server and a Upload button that
posts back the file selected, the purpose of the drop down is to choose the
directory where the file is saved in...
 
K

Karl Seguin

Siamak:
Just a guess, but are you rebinding on PostBack? If so you could be
overwriting the selection made by the user. Make sure to wrap the binding
code inside of an

if (!Page.IsPostBack){
ddlName.DataSource = xxx;
ddlName.DataBind();
}

Karl
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top