Skip to main content

How to sort DropDownList items in descending order in asp.net


DropDownList items sort order by descending



DropDownList is an asp.net list web server control that allow us to select one item at a time
from a drop-down-list. by default dropdownlist items are not sorted. to provide better user experience, web
developer wants to create a sorted dropdownlist. the following asp.net c# example code demonstrate us how can
we render a dropdownlist server control which items are descending sorted.






DropDownList server control have no built in property or method to sort its items descending or ascending order. we need to
apply few techniques to display a sorted dropdownlist programmatically.






At first we create a generic list which data type is ListItem. now we populate the generic list from dropdownlist
items collection. next we sort the list by descending order using Linq OrderByDescending() extension method. finally we clear the
dropdownlist items, and repopulate the dropdownlist with the sorted generic list. now we can display a descending sorted
dropdownlist on web browser. sorting applied for first page load time only, because we track the postback using page IsPostBack property.




dropdownlist-sort-order-by-descending.aspx



<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>

<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//create a generic list data type ListItem
List<ListItem> list = new List<ListItem>();

foreach (ListItem litem in DropDownList1.Items)
{
//add ListItem to generic list
list.Add(litem);
}

//sort list items by item text descending
List<ListItem> sorted = list.OrderByDescending(b => b.Text).ToList();

//clear dropdownlist items
DropDownList1.Items.Clear();

//repopulate dropdownlist with sorted items.
foreach (ListItem litem in sorted)
{
DropDownList1.Items.Add(litem);
}
}
}
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>asp.net c# dropdownlist sort order by descending</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2 style="color:MidnightBlue; font-style:italic;">
asp.net c# example - dropdownlist sort order by descending
</h2>
<hr width="550" align="left" color="Gainsboro" />
<br /><br />
<asp:DropDownList
ID="DropDownList1"
runat="server"
AutoPostBack="true"
Width="350"
Font-Size="X-Large"
Font-Names="Comic Sans MS"
>
<asp:ListItem Text="Grey Goshawk" Value="1"></asp:ListItem>
<asp:ListItem Text="African Harrier Hawk" Value="2"></asp:ListItem>
<asp:ListItem Text="Northern Goshawk" Value="3"></asp:ListItem>
<asp:ListItem Text="Common Black Hawk" Value="4"></asp:ListItem>
<asp:ListItem Text="Philippine Eagle" Value="5"></asp:ListItem>
</asp:DropDownList>
</div>
</form>
</body>
</html>




Popular posts from this blog

Restricting Jetpack Compose TextField to Numeric Input Only

Jetpack Compose has revolutionized Android development with its declarative approach, enabling developers to build modern, responsive UIs more efficiently. Among the many components provided by Compose, TextField is a critical building block for user input. However, ensuring that a TextField accepts only numeric input can pose challenges, especially when considering edge cases like empty fields, invalid characters, or localization nuances. In this blog post, we'll explore how to restrict a Jetpack Compose TextField to numeric input only, discussing both basic and advanced implementations. Why Restricting Input Matters Restricting user input to numeric values is a common requirement in apps dealing with forms, payment entries, age verifications, or any data where only numbers are valid. Properly validating input at the UI level enhances user experience, reduces backend validation overhead, and minimizes errors during data processing. Compose provides the flexibility to implement ...

jetpack compose - TextField remove underline

Compose TextField Remove Underline The TextField is the text input widget of android jetpack compose library. TextField is an equivalent widget of the android view system’s EditText widget. TextField is used to enter and modify text. The following jetpack compose tutorial will demonstrate to us how we can remove (actually hide) the underline from a TextField widget in an android application. We have to apply a simple trick to remove (hide) the underline from the TextField. The TextField constructor’s ‘colors’ argument allows us to set or change colors for TextField’s various components such as text color, cursor color, label color, error color, background color, focused and unfocused indicator color, etc. Jetpack developers can pass a TextFieldDefaults.textFieldColors() function with arguments value for the TextField ‘colors’ argument. There are many arguments for this ‘TextFieldDefaults.textFieldColors()’function such as textColor, disabledTextColor, backgroundColor, cursorC...

jetpack compose - Image clickable

Compose Image Clickable The Image widget allows android developers to display an image object to the app user interface using the jetpack compose library. Android app developers can show image objects to the Image widget from various sources such as painter resources, vector resources, bitmap, etc. Image is a very essential component of the jetpack compose library. Android app developers can change many properties of an Image widget by its modifiers such as size, shape, etc. We also can specify the Image object scaling algorithm, content description, etc. But how can we set a click event to an Image widget in a jetpack compose application? There is no built-in property/parameter/argument to set up an onClick event directly to the Image widget. This android application development tutorial will demonstrate to us how we can add a click event to the Image widget and make it clickable. Click event of a widget allow app users to execute a task such as showing a toast message by cli...