How to: set the selected "drop down list" value at run time.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I need to set the selected drop down list value at run time. I am aware of
the method "SelectIndex" however this works only if you know the precise
location of the value within the ListItem collection. Otherwise, what is the
recommended approach?

I have managed to set the appropriate value using the following loop:

for (int i = 1; i < ddlUsers.Items.Count; i++)
{
if (ddlUsers.Items.ToString() == sUserId)
{
ddlUsers.SelectedIndex = i;
break;
}
}

However, I wonder if there's an easier way than this "home-made" solution?
Thanks.
 
sorry SelectedValue

Sam Martin said:
how about
this.ddlUsers.SelectedItem = sUserId;


HTH
sam

charliewest said:
I need to set the selected drop down list value at run time. I am aware of
the method "SelectIndex" however this works only if you know the precise
location of the value within the ListItem collection. Otherwise, what is
the
recommended approach?

I have managed to set the appropriate value using the following loop:

for (int i = 1; i < ddlUsers.Items.Count; i++)
{
if (ddlUsers.Items.ToString() == sUserId)
{
ddlUsers.SelectedIndex = i;
break;
}
}

However, I wonder if there's an easier way than this "home-made"
solution?
Thanks.

 
Thanks Sam. The strange thing is that the intellisense description says this
property is read only? But it does work!

Sam Martin said:
sorry SelectedValue

Sam Martin said:
how about
this.ddlUsers.SelectedItem = sUserId;


HTH
sam

charliewest said:
I need to set the selected drop down list value at run time. I am aware of
the method "SelectIndex" however this works only if you know the precise
location of the value within the ListItem collection. Otherwise, what is
the
recommended approach?

I have managed to set the appropriate value using the following loop:

for (int i = 1; i < ddlUsers.Items.Count; i++)
{
if (ddlUsers.Items.ToString() == sUserId)
{
ddlUsers.SelectedIndex = i;
break;
}
}

However, I wonder if there's an easier way than this "home-made"
solution?
Thanks.


 
Back
Top