Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a ASP.NET Expert!

Join 416,721 ASP.NET Programmers for FREE! Get instant access to thousands of ASP.NET experts, tutorials, code snippets, and more! There are 2,869 people online right now.Registration is fast and FREE... Join Now!



Sorting a DropDownList Rate Topic: -----

#1 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • Icon

Reputation: 1401
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07


Dream Kudos: 12950

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Share |

Sorting a DropDownList

Posted 28 February 2008 - 11:12 PM

In my web application I have a DropDownList that is populated from a DataSet. Once the DropDownList is bound Im attempting to sort it using the following code, but it isnt working, and I cant figure out why


//first we bind the DropDwnList
DataSet domains = util.BuildDataSet(Server.MapPath("supported_domains.txt"), "Domains", "\t");
supported_domain.DataSource = domains;
supported_domain.DataTextField = domains.Tables[0].Columns[0].ToString();
supported_domain.DataValueField = domains.Tables[0].Columns[0].ToString();
supported_domain.DataBind();

//now we will sort the DropDownList
ListItem[] sorted = new ListItem[supported_domain.Items.Count];
//Response.Write(sorted.Length);
for (int i = 0; i < sorted.Length; i++)
{
    sorted[i] = supported_domain.Items[i];
}
Array.Sort(sorted);
supported_domain.Items.Clear();
supported_domain.Items.AddRange(sorted);


Was This Post Helpful? 0
  • +
  • -


#2 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • Icon

Reputation: 29
  • View blog
  • Posts: 824
  • Joined: 14-November 07


Dream Kudos: 175

Re: Sorting a DropDownList

Posted 28 February 2008 - 11:27 PM

It's probably something to do with the data binding, so you can't your own entries to it.

Have you tried setting the Sorted property to true? (assuming the DropDownList supports it).

This post has been edited by Nayana: 28 February 2008 - 11:28 PM

Was This Post Helpful? 0
  • +
  • -

#3 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • Icon

Reputation: 1401
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07


Dream Kudos: 12950

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Sorting a DropDownList

Posted 28 February 2008 - 11:37 PM

A DropDownList doesn't support sorting. Guess I can try to sort the items before adding them to the DataSet, but that just seems like a ton of work.

Well the code works on a non data bound DropDownList, so back to the drawing board lol
Was This Post Helpful? 0
  • +
  • -

#4 Nayana  Icon User is offline

  • DIC Hawk - 나야나 नयन:
  • Icon

Reputation: 29
  • View blog
  • Posts: 824
  • Joined: 14-November 07


Dream Kudos: 175

Re: Sorting a DropDownList

Posted 29 February 2008 - 01:29 AM

I don't actually have ASPX to play around with, so bear with me...

Would it be possible to unbind the DropDownList, then copy the array items back into it?

Does the array even get the items in the first place?
Was This Post Helpful? 0
  • +
  • -

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • Icon

Reputation: 1401
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07


Dream Kudos: 12950

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Sorting a DropDownList

Posted 29 February 2008 - 09:01 AM

Yeah the Array gets the items, I had it print out it's length and it was the correct length
Was This Post Helpful? 0
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users