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

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

P: n/a
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[i].ToString() == sUserId)
{
ddlUsers.SelectedIndex = i;
break;
}
}

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

Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
how about
this.ddlUsers.SelectedItem = sUserId;
HTH
sam

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.com...
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[i].ToString() == sUserId)
{
ddlUsers.SelectedIndex = i;
break;
}
}

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

Nov 16 '05 #2

P: n/a
Set the SelectedValue property of the drop down list.

NuTcAsE

Nov 16 '05 #3

P: n/a
sorry SelectedValue

"Sam Martin" <sa*********@yahoo.co.uk> wrote in message
news:uH**************@TK2MSFTNGP14.phx.gbl...
how about
this.ddlUsers.SelectedItem = sUserId;
HTH
sam

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.com...
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[i].ToString() == sUserId)
{
ddlUsers.SelectedIndex = i;
break;
}
}

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


Nov 16 '05 #4

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

"Sam Martin" wrote:
sorry SelectedValue

"Sam Martin" <sa*********@yahoo.co.uk> wrote in message
news:uH**************@TK2MSFTNGP14.phx.gbl...
how about
this.ddlUsers.SelectedItem = sUserId;
HTH
sam

"charliewest" <ch*********@discussions.microsoft.com> wrote in message
news:5B**********************************@microsof t.com...
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[i].ToString() == sUserId)
{
ddlUsers.SelectedIndex = i;
break;
}
}

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



Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.