Problems with DropDownList

G

Guest

I've been experiencing problems with getting the selected value from a DropDownList. I'm using a codebehind-solution and I'm filling a DropDownList with ListItems in the Page_Load method and all works fine. The problem arises when I try to get the selected item in the DropDownList in an event handler method mapped to a button. When I execute the following line
dropDownList.SelectedValu
I always get the value from the first ListItem
The funny thing is that I've got no problems with getting the entered text in for example a text box
What have I missed

The first line of the aspx-page looks like this
<%@ Page language="c#" Codebehind="supportsession.aspx.cs" AutoEventWireup="false" Inherits="Support_Ver2.supportsession" %

and I'm not using AutoPostBack in the DropDownList control.
 
M

Martin Dechev

Hi, Michael Strid,

Are you sure you are not setting the SelectedIndex (or SelectedItem or
SelectedValue) on every Page_Load? Can you post a short example that
illustrates the problem?

Greetings
Martin
Michael Strid said:
I've been experiencing problems with getting the selected value from a
DropDownList. I'm using a codebehind-solution and I'm filling a DropDownList
with ListItems in the Page_Load method and all works fine. The problem
arises when I try to get the selected item in the DropDownList in an event
handler method mapped to a button. When I execute the following line
dropDownList.SelectedValue
I always get the value from the first ListItem.
The funny thing is that I've got no problems with getting the entered text in for example a text box.
What have I missed?

The first line of the aspx-page looks like this:
<%@ Page language="c#" Codebehind="supportsession.aspx.cs"
AutoEventWireup="false" Inherits="Support_Ver2.supportsession" %>
 
H

hb

Michael Strid said:
I've been experiencing problems with getting the selected value from a
DropDownList. I'm using a codebehind-solution and I'm filling a DropDownList
with ListItems in the Page_Load method and all works fine. The problem
arises when I try to get the selected item in the DropDownList in an event
handler method mapped to a button. When I execute the following line
dropDownList.SelectedValue
I always get the value from the first ListItem.
The funny thing is that I've got no problems with getting the entered text in for example a text box.
What have I missed?

The first line of the aspx-page looks like this:
<%@ Page language="c#" Codebehind="supportsession.aspx.cs"
AutoEventWireup="false" Inherits="Support_Ver2.supportsession" %>
and I'm not using AutoPostBack in the DropDownList control.

One common mistake would be to add the ListItems every time the page loads.
You should place that code inside an "if ( !Page.IsPostBack ) { }" block, so
the Items are added once the first time the page loads, and the viewstate
should take care of any postbacks.
 
J

john.winsor

I have nearly the identical problem.

Putting the list fill in !postback does take care of propogating the last choice, but for some reason setting the selectedvalue does not have an effect, though it throws no error either.

Setting the selected value to "offices" has no effect. The dropdown defaults to none selected every time.

Ideas? Code below...

Hashtable SearchDomains = new Hashtable(3);
SearchDomains.Add("Personnel","personnel");
SearchDomains.Add("Offices","offices");
SearchDomains.Add("Units","units");

SearchDomain.DataSource = SearchDomains;
SearchDomain.DataTextField = "Key";
SearchDomain.DataValueField = "Value";
try
{
SearchDomain.SelectedValue = "offices";
}
catch (Exception ex)
{
SearchDomain.SelectedValue = null;
}
SearchDomain.DataBind();
 
B

Bruno Sirianni

try this :

Hashtable SearchDomains = new Hashtable(3);
SearchDomains.Add("Personnel","personnel");
SearchDomains.Add("Offices","offices");
SearchDomains.Add("Units","units");

SearchDomain.DataSource = SearchDomains;
SearchDomain.DataTextField = "Key";
SearchDomain.DataValueField = "Value";

SearchDomain.DataBind(); //first bind

try
{
SearchDomain.SelectedValue = "offices"; //then select the value!
}
catch
{
SearchDomain.SelectedValue = null;
}




I have nearly the identical problem.

Putting the list fill in !postback does take care of propogating the last
choice, but for some reason setting the selectedvalue does not have an
effect, though it throws no error either.
Setting the selected value to "offices" has no effect. The dropdown
defaults to none selected every time.
Ideas? Code below...

Hashtable SearchDomains = new Hashtable(3);
SearchDomains.Add("Personnel","personnel");
SearchDomains.Add("Offices","offices");
SearchDomains.Add("Units","units");

SearchDomain.DataSource = SearchDomains;
SearchDomain.DataTextField = "Key";
SearchDomain.DataValueField = "Value";
try
{
SearchDomain.SelectedValue = "offices";
}
catch (Exception ex)
{
SearchDomain.SelectedValue = null;
}
SearchDomain.DataBind();
engine supports Post Alerts, Ratings, and Searching.
 

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

Top