Link to home
Start Free TrialLog in
Avatar of deloused
deloused

asked on

Check if item is in data

I need to check if an item is in my datasource on post back.  I am using C# with asp.net.  What I have is a cascading dropdown, where the first dropdown's selected item(projects) determines the second dropdown(users).  I would like to store the user and keep it "selected" if they exist in another project.  

My psuedo code looks like this, but I'm not sure about syntax:
On Postback
string selectedusername = dropdown2.selecteditem;

if (selectedusername is in datasource items)
{
    dropdown1.selecteditem = selectedusername
}
else

thanks in advance
Avatar of GreymanMSC
GreymanMSC


String selectedusername = dropdown2.SelectedValue;
if (dropdown1.Items.FindByValue(selectedusername))
{
    dropdown1.SelectedValue = selectedusername
}
else
{
    'do some other stuff
}

Open in new window

Avatar of deloused

ASKER

I'm getting the error:
Cannot implicitly convert type 'System.Web.UI.WebControls.ListItem' to 'bool'

Why is it thinking it's a bool? Here's my code:
String PreviouslySelectedUser = UserList.SelectedValue;
 
      if (UserList.Items.FindByValue(PreviouslySelectedUser))
      {
          UserList.DataBind();
          UserList.SelectedValue = PreviouslySelectedUser;
      }
      else
      {
          UserList.DataBind();
          //do some other stuff
      }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of GreymanMSC
GreymanMSC

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
Ok, I'm getting really close now but I'm getting an error.  

'UserList' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value

The check for Null values is working, but the problem is that there will always be an initial value but the next value might not be in the list.  What I need to check is something like this:

if (UserList.Items has(or contains) the item(PreviouslySelectedUser))
{
     UserList.SelectedValue = PreviouslySelectedUser;
{
else
{
      UserList.SelectedIndex = 0;
}

does that make sense?  It's really close, but with the last code you gave me it's always iterating the if and never the else.  
Thanks for your help!  I figured it out!  I had something missing, your code worked prefectly!