Working with Dropdownlist


It quite easy to work with dropdown list. You just need to drag-and drop it. But let just work with it.

Suppose, we want to add an datasource which have multiple fields, or coming from database SELECT with multiple fields.

Lets we have a List<User> from Facebook which have properties but we want to see the Name as dropdown text which is in the User object written as “name” and set the value to UserId which is written as “uid”.

image

Now what we could do is just set the datasource to the List<User> and then specify the “DataTextField” and “DataValueField”.

IList<user> friendList = Master.Api.Friends.GetUserObjects();           
          ddlFriends.DataSource = friendList;
          ddlFriends.DataTextField = "name";
          ddlFriends.DataValueField = "uid";
          ddlFriends.DataBind();

image

Ok, Also if you want to make the text color different in every row or change the row background you can use the following code section: after the “DATABIND()” method

For text only:

ddlFriends.Items[count].Attributes.Add("style", "color:#FC8105");

For Background:

ddlFriends.Items[count].Attributes.Add("style", "background-color:#51D9E8");

.

Posted on September 4, 2010, in ASP.net, Asp.net User Control, C#. Bookmark the permalink. Leave a Comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 54 other followers