[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 169
  • Last Modified:

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
0
deloused
Asked:
deloused
  • 6
  • 2
1 Solution
 
GreymanMSCCommented:

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

Open in new window

0
 
delousedAuthor Commented:
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

0
 
GreymanMSCCommented:
Ah.  Sorry.  My fault.  The statement is missing a !=null on the end.  Opps!

The FindByValue method returns the found ListItem object (or null if not found),  The if statement requires a boolean condition.  This it is trying to implicitly convert the found list object.  


      String PreviouslySelectedUser = UserList.SelectedValue;
 
      UserList.DataBind(); //Bind before you search to reselect.
      if (UserList.Items.FindByValue(PreviouslySelectedUser)!=null)
      {
          UserList.SelectedValue = PreviouslySelectedUser;
      }
      else
      {
          //do some other stuff
      }

Open in new window

0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
delousedAuthor Commented:
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.  
0
 
delousedAuthor Commented:
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.  
0
 
delousedAuthor Commented:
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.  
0
 
delousedAuthor Commented:
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.  
0
 
delousedAuthor Commented:
Thanks for your help!  I figured it out!  I had something missing, your code worked prefectly!  
0

Featured Post

Learn to develop an Android App

Want to increase your earning potential in 2018? Pad your resume with app building experience. Learn how with this hands-on course.

  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now