Can't set SelectedValue property on ASP.NET DropDown

  • Thread starter Thread starter Steven
  • Start date Start date
S

Steven

Hi there,

I am having a problem with an ASP.NET DropDown list - whenever I set the
SelectedValue (or SelectedIndex) property, when the page displays, only the
first item is ever selected. This is a databound dropdown, and I have
executed the DataBind() method prior to trying to set the selectedvalue
property. Both are being done in the Page_Load method, and only if the page
is not posting back.

Any ideas would be appreciated.

-Steven
 
try using the FindByValue method on the items collection and then set the
Selected property to true:

ddlMyList.Items.FindByValue("stringforselectedvalue").Selected = true;

hth
 
I've tried that too..

my code is as follows (BindData() gets called from the Page_Load method):

private void BindData()
{
lstAssociations.DataSource =
BusinessLogic.ProductAssociations.Instance().GetAssociations();
lstAssociations.DataTextField = "DisplayText";
lstAssociations.DataValueField = "AssociationID";
lstAssociations.DataBind();

lstAssociations.SelectedValue = Request.QueryString["associationID"];

// Also tried
//
lstAssociations.Items.FindByValue(Request["associationID"].ToString().Trim()
).Selected = true;
}

Any ideas?
 
For anyone who is interseted, the problem was that I was setting the
selectedvalue property AFTER binding the data - it has to be set before hand
(before you set the datasource and call DataBind())
Steven said:
I've tried that too..

my code is as follows (BindData() gets called from the Page_Load method):

private void BindData()
{
lstAssociations.DataSource =
BusinessLogic.ProductAssociations.Instance().GetAssociations();
lstAssociations.DataTextField = "DisplayText";
lstAssociations.DataValueField = "AssociationID";
lstAssociations.DataBind();

lstAssociations.SelectedValue = Request.QueryString["associationID"];

// Also tried
//
lstAssociations.Items.FindByValue(Request["associationID"].ToString().Trim()
).Selected = true;
}

Any ideas?

Giri said:
try using the FindByValue method on the items collection and then set the
Selected property to true:

ddlMyList.Items.FindByValue("stringforselectedvalue").Selected = true;

hth

only
the the
page
 
Back
Top