By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,865 Members | 1,717 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,865 IT Pros & Developers. It's quick & easy.

DropDownList always returning the first item

P: n/a
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 .

Nov 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Did you try “name = ddlName.SelectedItem.Value”.
ToString() is probably not the selected ListItem value.

Shaw
"Siamak Zahedi" wrote:
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 .

Nov 19 '05 #2

P: n/a
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...
"Shaw" <Sh**@discussions.microsoft.com> wrote in message
news:DF**********************************@microsof t.com...
Did you try "name = ddlName.SelectedItem.Value".
ToString() is probably not the selected ListItem value.

Shaw
"Siamak Zahedi" wrote:
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 .

Nov 19 '05 #3

P: n/a
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

--
MY ASP.Net tutorials
http://www.openmymind.net/
"Siamak Zahedi" <T_*@telus.net> wrote in message
news:4TeKd.52788$Qb.23905@edtnps89...
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 .

Nov 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.