469,336 Members | 5,570 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

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

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
4 37260
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
Set the SelectedValue property of the drop down list.

NuTcAsE

Nov 16 '05 #3
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
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.

Similar topics

2 posts views Thread by Kyle Blaney | last post: by
3 posts views Thread by Mike Collins | last post: by
4 posts views Thread by nz mark in aus | last post: by
LuiePL
1 post views Thread by LuiePL | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.