Dropdown reselection

  • Thread starter Thread starter tshad
  • Start date Start date
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
 
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
 
Hi,

try this:

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

Any doubt, post your comment.

S.
 
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.
 
Back
Top