.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Post New Resource Bookmark and Share   

Sort records in a dropdownlist

Posted By:satyapriyanayak       Posted Date: March 11, 2013    Points: 40    Category:    URL: http://www.dotnetspark.com  

Sort records in a dropdownlist
 

Default.aspx code

 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

 

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

   

    </div>

    <asp:DropDownList ID="DropDownList1" runat="server">

    </asp:DropDownList>

    </form>

</body>

</html>

 

Default.aspx.cs code

 

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

 

public partial class Default : System.Web.UI.Page

{

    protected void Page_Load(object sender, EventArgs e)

    {

        DropDownList1.Items.Add("Raj");

        DropDownList1.Items.Add("Berry");

        DropDownList1.Items.Add("Arun");

        DropDownList1.Items.Add("Crish");

        dropSort(DropDownList1);

 

    }

    private void dropSort( DropDownList objdropSort)

    {

        ArrayList t1 = new ArrayList();

        ArrayList t2 = new ArrayList();

 

 

        foreach (ListItem l1 in objdropSort.Items)

        {

            t1.Add(l1.Text);

        }

 

        t1.Sort();

 

 

        foreach (object ob1 in t1)

        {

            string value = objdropSort.Items.FindByText(ob1.ToString()).Value;

            t2.Add(value);

        }

        objdropSort.Items.Clear();

 

        for (int i = 0; i < t1.Count; i++)

        {

            ListItem t3 = new ListItem(t1[i].ToString(), t2[i].ToString());

            objdropSort.Items.Add(t3);

        }

    }

 

}



     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend