0

I have project which need a modification. I need to sort column in table. The problem is that column contains label or dropdown list depending from outside data to which i don't have acces.

<asp:TemplateField HeaderText="System" >
<ItemTemplate>
<asp:Label ID="LabelSystem" runat="server" Text="Failure" />
<asp:DropDownList ID="DropDownListSystem" runat="server" DataTextField="name" />
</ItemTemplate>
</asp:TemplateField>

Can someone assist? I need to sort element depending of the type of element ordered by name. So for exp. FirstLabel SecondLabel FirstDropDownList SecondDropDownList ThirdDropDownList

To better explain what is on my mind I prepared the illustrations :)

Table before sorting:

Before

Now after pressing "System" it should sort column in one of this two ways:

After

kendzi
  • 311
  • 2
  • 6
  • 22

2 Answers2

0

Similar to this question here - Sorting dropdown list using Javascript

$("#id").html($("#id option").sort(function (a, b) { return a.text > b.text; }

Community
  • 1
  • 1
Jon La Marr
  • 1,358
  • 11
  • 14
0

Where is the datasource for you drop down list? if it is being populated by a SQL Server Query you can simply sort it within the query itself before it reaches the drop down list.

Like this

Create proc spGetNames
as
Begin
    SELECT *
    FROM NamesList
    ORDER BY Name
End

What exactly are you trying to do? adding template fields in specific order will from top to bottom will render them from left to right based on where they are in that order. If you want to sort a specific column in a table then the solution i provided is adequate. Maybe you want to sort the table based on the result of a certain condition being met? if so you can pass a parameter through your sql query that allows this to happen e.g.

Create proc spGetNames
@Condition nvarchar(50)
as
Begin
    SELECT *
    FROM NamesList
    ORDER BY @Condition
End
Master Yoda
  • 4,334
  • 10
  • 43
  • 77
  • This is if you are calling the query using a stored procedure but simply remove the first three lines and the last line and you have your sql query for use in straight forward ADO.Net implementation – Master Yoda Aug 21 '13 at 15:31
  • But this refers only to drop down list and I need firstly to sort column in table. – kendzi Aug 21 '13 at 15:38
  • What exactly are you trying to do? adding template fields in specific order will from top to bottom will render them from left to right based on where they are in that order. – Master Yoda Aug 21 '13 at 15:47
  • You will have to sort first by what i presume to be a labels text, then sort by the text inside the drop down control. C# provides a sort alorithm that should be able to accomplish this with a little bit of improvisation on your part: http://www.sorting-algorithms.com/ quick sort or merge sort could fix this by passing it a string as a generic type then you would just have to set a flag to determine if the control is either a label or drop down list – Master Yoda Aug 23 '13 at 15:56