Dropdown reselection

T

tshad

If I have a dropdown that is going to repopulate, I want to be able to put
the selection back (if it is still there).

For example:

oldValue = ddlClient.SelectedValue;
GetClientNames();
ddlClient.Items.FindByValue(oldValue).Selected = true;

This works fine if the selection is still there. If not you get an Object
not there error.

If you do this:

oldValue = ddlClient.SelectedValue;
GetClientNames();
if(ddlClient.Items.FindByValue(oldValue) != null)
ddlClient.Items.FindByValue(oldValue).Selected = true;

You get a multiple selection error.

Does the if statement actually select it so that I can actually do:

oldValue = ddlClient.SelectedValue;
GetClientNames();
ddlClient.Items.FindByValue(oldValue);

and it will select it if there, otherwise be at the 1st item in the list.

If so, then I assume that the following statement is really never a good
idea:

ddlClient.Items.FindByValue(oldValue).Selected = true;

Am I right here?

Thanks,

Tom
 
T

tshad

tshad said:
If I have a dropdown that is going to repopulate, I want to be able to put
the selection back (if it is still there).

For example:

oldValue = ddlClient.SelectedValue;
GetClientNames();
ddlClient.Items.FindByValue(oldValue).Selected = true;

This works fine if the selection is still there. If not you get an Object
not there error.

If you do this:

oldValue = ddlClient.SelectedValue;
GetClientNames();
if(ddlClient.Items.FindByValue(oldValue) != null)
ddlClient.Items.FindByValue(oldValue).Selected = true;

You get a multiple selection error.

Does the if statement actually select it so that I can actually do:

oldValue = ddlClient.SelectedValue;
GetClientNames();
ddlClient.Items.FindByValue(oldValue);

and it will select it if there, otherwise be at the 1st item in the list.

Actually, I am wrong here.

This does nothing. It will just put you back to the first item in the list.
But it does seem to set something since I can't do:

if(ddlClient.Items.FindByValue(oldValue) != null)
ddlClient.Items.FindByValue(oldValue).Selected = true;

Which would seem to be the way to do it (if the first line is not setting
the selection).

So how do you handle this?

Thanks,

Tom
 
S

Segundo Serrano

Hi,

try this:

ddlClient.SelectedIndex = ddlClient.Items.IndexOf(ddlClient.Items.FindByValue(oldValue));

Any doubt, post your comment.

S.
 
T

tshad

That works.

But why doesn't this work:

if(ddlClient.Items.FindByValue(oldValue) != null)
ddlClient.Items.FindByValue(oldValue).Selected = true;

Aren't they doing the same thing?

Thanks,

Tom
"Segundo Serrano" <sserrano[at]jabs[dot]com[dot]pe> wrote in message Hi,

try this:

ddlClient.SelectedIndex = ddlClient.Items.IndexOf(ddlClient.Items.FindByValue(oldValue));

Any doubt, post your comment.

S.
 

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

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top