Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-04-06
5
Medium Priority
?
677 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 2000 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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

715 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