Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sort order of drop down list

Status
Not open for further replies.

Ranvier

Programmer
Jun 17, 2004
73
GB
Hello,

I need to know how to order a list alphabetically in a drop down list

ddUserList contains the contents of the list

This is my html code:

<label class="fixed-label" for="text1">Who are you? <span>*</span></label>
<asp:DropDownList ID="ddUserList" runat="server" CssClass="select" AutoPostBack="true" OnSelectedIndexChanged="ddUserList_SelectedIndexChanged"></asp:DropDownList>


At the moment they are just randomly ordered. Any help is appreciated.

Thanks
 
If you are binding the ddl to some sort of datasource, then order the data in the datasource before binding. For example, if you select the data using a stored prodedure, then order the data there.
 
Thanks for the advice but I am using a web service to get my data and unfortunately it does not have the capability to order the data which is why I need to do it from the drop down list. Is this possible from the code I posted?

Thanks
 
Thanks for the advice but I am using a web service to get my data and unfortunately it does not have the capability to order the data which is why I need to do it from the drop down list. Is this possible from the code I posted?
the point between receiving the data and loading the dropdown. that is where you sort the data.

the code you posted above tells use nothing about how you get the data from the webserivce, or how you pass the collection into the dropdownlist.

it doesn't matter if you where to send the data to a dropdownlist, listbox, repeater, etc. at this point you are just presenting data, how it got there doesn't matter.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
the point between receiving the data and loading the dropdown. that is where you sort the data.
If you receive the data from the data base, then that is where the sorting should be done. sorting is a database concern. However, since you are using a webservice, you will need to find a different way.
 
the point between receiving the data and loading the dropdown. that is where you sort the data.
If you receive the data from the data base, then that is where the sorting should be done. sorting is a database concern.
not really. the UI & sequential logic are concerned about ordered data. other than that it's just data, order isn't a factor. the database persists and retrieves data. the UI/logic may be able to inform the database of a predetermined sort, but the database is not solely responsible for sorting.

the amount of data is also a factor. if I'm displaying a small number of records on the screen sorting at the database, or client code is irrelevant. I may be saving milliseconds. that's I'm not concerned about. I would be concerned about the amount of time it takes to retrieve the data I would solve that problem by loading the data in chunks and caching on the client (not browser client, code client).

However, since you are using a webservice, you will need to find a different way.
hence sorting at the point the results are consumed by code.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
not really. the UI & sequential logic are concerned about ordered data. other than that it's just data, order isn't a factor. the database persists and retrieves data. the UI/logic may be able to inform the database of a predetermined sort, but the database is not solely responsible for sorting.
I'm not going to go down this road again with you. DB opeations belong in the DB. I am not saying the DB is soley responsible for sorting data, however, it is the most logical and easiest to impliment. I do see your point about the separation because it is a web service.
 
OK. As my datasource, in this case a web service cannot sort my data I have to do it after I retrieved it.

This is how i populate my list (System.Web.UI.WebControls.ListItem)


ListItem itm = new ListItem();
itm.Text = cnt.entityId;
ddUserList.Items.Add(itm);

My list would contain no more than 100 names so i dont have to worry about heavy loads of data.

Any ideas how I sort this/

thanks
 
assuming the result of calling the web service is a collection of strings.
Code:
List<string> data = new List<string>();
data.AddRange(GetResultsFromWebService());
data.Sort(); //because they are strings the default sort works

ddUserList.DataSource = data;
ddUserList.DataBind();
where GetResultsFromWebService() will return IEnumerable<string> and makes the call to the web service

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top