Solved

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

Posted on 2007-04-06
5
643 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
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

867 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now