Solved

Using a drop down list in ASP.NET with ListItemCollection.

Posted on 2007-04-06
5
669 Views
Last Modified: 2013-11-26
Hello, in a class called UserNames, i have the following

  public static ListItemCollection GetUserListItems(ProfileCommon Profile) {
    ListItemCollection Items = new ListItemCollection();

    System.Web.Profile.ProfileInfoCollection pic = System.Web.Profile.ProfileManager.GetAllProfiles(System.Web.Profile.ProfileAuthenticationOption.All);
    foreach (System.Web.Profile.ProfileInfo prof in pic) {
      ListItem Item = new ListItem();

      Item.Value = Profile.GetProfile(prof.UserName.ToString()).UserName.ToString();

      Item.Text = Profile.GetProfile(prof.UserName.ToString()).LastName.ToString() + ", " +
        Profile.GetProfile(prof.UserName.ToString()).FirstName.ToString() + " (" +
        Profile.GetProfile(prof.UserName.ToString()).UserName.ToString() + ")";
      Items.Add(Item);
    }
    return Items;
  }

in my web form code i have the following under page_load

      drpOwner.DataSource = UserNames.GetUserListItems(Profile);
      drpOwner.DataBind();
      Response.Write(drpClient.SelectedValue.ToString());  //<--- Shows the full name, not just what should be (should just be a username) in the value from the static function (above)
      drpClient.SelectedValue = "test"; //<-- Comes back as saying this is a null item.  Should not be.
      Response.Write(drpClient.SelectedItem.Value.ToString());
0
Comment
Question by:wd006451
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 1

Author Comment

by:wd006451
ID: 18866830
Also is what im trying to do is just set the default username to the username of "test"
0
 
LVL 35

Accepted Solution

by:
mrichmon earned 500 total points
ID: 18867153
Try:

drpOwner.DataSource = UserNames.GetUserListItems(Profile);
drpOwner.datatextfield = "text";
drpOwner.datavaluefield="Value";
drpOwner.DataBind();
0
 
LVL 1

Author Comment

by:wd006451
ID: 18867511
Thanks, can you exlpain that a little bit more?  It worked, but im not sure what/why im doing that?
0
 
LVL 14

Expert Comment

by:existenz2
ID: 18867783
0
 
LVL 35

Expert Comment

by:mrichmon
ID: 18867970
Basically you are assigning which filed in the ListItemCollection should be used as the text and which field should be used as the value.

You would think that since the text field is named text and the value field named value that it would work automatically, but it does not when binding to a ListItemCollection.
0

Featured Post

 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question