Friday, 4 January 2013

DropDownList with Custom Sorted List Binding

DropDownList-with-custom-object-Sorted-List-Binding


Step1) Create a ASP.NET Empty WebSite using C# 4.0/4.5
Step 2)  Add new Page name it as Default.aspx
Step3) Add DropDownList and Label

        <span class="newStyle1">Select Profile:</span>
<asp:DropDownList
ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_
SelectedIndexChanged" CssClass="newStyle2"></asp:DropDownList><br />

<asp:Label ID="Label1" runat="server"
Text="Label" CssClass="lblClass"
Width="557px" Height="201px"></asp:Label>

Step 4) Add a class called Profile

   public class Profile
    {
        public int ProfileID { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public int Age { get; set; }
        public DateTime JoinedDate { get; set; }
        public String EmailID { get; set; }
        public String ZIPCode { get; set; }
        public String Country { get; set; }
        public override string ToString()
        {
            return String.Format(
                "ProfileID={0},FirstName={1},LastName={2},Age={3},JoinedDate={4},Email={5},Zip={6},Country={7}",
                ProfileID,FirstName,LastName,Age,
                JoinedDate, EmailID,ZIPCode,Country
                );
        }
    }

Step 5) Build List of Sample Data for Profile List
      
private void BuildProfile()
        {
            if (profiles.Count == 0)
            {
                profiles.Add(new Profile { Age = 21, Country = "USA", EmailID = "jhon@gmail.com", FirstName = "jhon", LastName = "Peter", ProfileID = 1, ZIPCode = "94086-2422", JoinedDate = new System.DateTime(1998, 1, 12) });
                profiles.Add(new Profile { Age = 18, Country = "USA", EmailID = "macy@yahoo.com", FirstName = "macy", LastName = "William", ProfileID = 2, ZIPCode = "67897-2422", JoinedDate = new System.DateTime(2000, 11, 1) });
                profiles.Add(new Profile { Age = 28, Country = "UK", EmailID = "alex@gmail.com", FirstName = "alex", LastName = "Samuel", ProfileID = 3, ZIPCode = "STHL 1ZZ", JoinedDate = new System.DateTime(2002, 10, 9) });
                profiles.Add(new Profile { Age = 35, Country = "UK", EmailID = "bearns@gmail.com", FirstName = "beans", LastName = "Jessica", ProfileID = 4, ZIPCode = "AA9A 9AA", JoinedDate = new System.DateTime(2004, 10, 12) });
                profiles.Add(new Profile { Age = 36, Country = "INDIA", EmailID = "prasad@gmail.com", FirstName = "prasad", LastName = "kanuturi", ProfileID = 5, ZIPCode = "110011", JoinedDate = new System.DateTime(1990, 1, 2) });
            }

        }

Step 6)  Sort List using LINQ to Objects in asp.net 3.0/3.5/4.0/4.5

 var query = from p in profiles
                            orderby p.FirstName
                            select p;

Step 7) Bind  "query" to DropDownList

               DropDownList1.DataSource = query;
                DropDownList1.DataTextField = "FirstName"; //select DataTextField as Firstname
                DropDownList1.DataBind();

Step 8) Add Default value to DropDownList
                DropDownList1.Items.Insert(0, "Select Profile");
Step 9)   Add DropDownList SelectedIndexChanged  with AutoPostback=true

Step 10) Display Data in the Label.

            String FirstName = DropDownList1.SelectedValue;
            Profile profile = profiles.SingleOrDefault(p => p.FirstName == FirstName);
            if (profile != null)
            {
                Label1.Text = "<center>" + profile.ToString().Replace(",", "<br/>") + "</center>"; ;
            }
Step 11)  Run the WebPage Default.aspx

DropDownList Custom Sorted List Binding
DropDownList Custom Sorted List Binding

Complete Source Code


Default.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DropDownList-DisplayMember.aspx.cs" Inherits="WebApplication1.DropDownList_DisplayMember" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .lblClass
        {
            background-color:teal;
            color:white;
            font-weight:bold;
            }
        .newStyle1
        {
            background-color: #808000;
            background-position: center top;
            color: #FFFFFF;
            font-weight: bolder;
            font-size: x-large;
            font-family: Batang;
            font-style: oblique;
            vertical-align: top;
        }
        .newStyle2
        {
            font-family: Georgia, "Times New Roman", Times, serif;
            font-size: large;
            font-weight: bold;
            background-color: #0000FF;
            color: #FFFFFF;
        }
        h1
        {
            color:aliceblue;
            background-color:black;
            width:500px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>DropDownList with Custom object Sorted List Binding in ASP.NET</h1>
        <span class="newStyle1">Select Profile:</span><asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" CssClass="newStyle2"></asp:DropDownList><br />
        <asp:Label ID="Label1" runat="server" Text="Label" CssClass="lblClass" Width="557px" Height="201px"></asp:Label>
    </div>
    </form>
</body>
</html>


Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebApplication1
{
    public partial class DropDownList_DisplayMember : System.Web.UI.Page
    {
        private List<Profile> profiles = new List<Profile>();

        protected void Page_Load(object sender, EventArgs e)
        {
            BuildProfile();
            if(!IsPostBack){

                var query = from p in profiles
                            orderby p.FirstName
                            select p;
                DropDownList1.DataSource = query;
                DropDownList1.DataTextField = "FirstName";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0, "Select Profile");
             
            }
        }

        protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            String FirstName = DropDownList1.SelectedValue;
            Profile profile = profiles.SingleOrDefault(p => p.FirstName == FirstName);
            if (profile != null)
            {
                Label1.Text = "<center>" + profile.ToString().Replace(",", "<br/>") + "</center>"; ;
            }

        }

        private void BuildProfile()
        {
            if (profiles.Count == 0)
            {
                profiles.Add(new Profile { Age = 21, Country = "USA", EmailID = "jhon@gmail.com", FirstName = "jhon", LastName = "Peter", ProfileID = 1, ZIPCode = "94086-2422", JoinedDate = new System.DateTime(1998, 1, 12) });
                profiles.Add(new Profile { Age = 18, Country = "USA", EmailID = "macy@yahoo.com", FirstName = "macy", LastName = "William", ProfileID = 2, ZIPCode = "67897-2422", JoinedDate = new System.DateTime(2000, 11, 1) });
                profiles.Add(new Profile { Age = 28, Country = "UK", EmailID = "alex@gmail.com", FirstName = "alex", LastName = "Samuel", ProfileID = 3, ZIPCode = "STHL 1ZZ", JoinedDate = new System.DateTime(2002, 10, 9) });
                profiles.Add(new Profile { Age = 35, Country = "UK", EmailID = "bearns@gmail.com", FirstName = "beans", LastName = "Jessica", ProfileID = 4, ZIPCode = "AA9A 9AA", JoinedDate = new System.DateTime(2004, 10, 12) });
                profiles.Add(new Profile { Age = 36, Country = "INDIA", EmailID = "prasad@gmail.com", FirstName = "prasad", LastName = "kanuturi", ProfileID = 5, ZIPCode = "110011", JoinedDate = new System.DateTime(1990, 1, 2) });
            }

        }


    }


    public class Profile
    {

        public int ProfileID { get; set; }

        public String FirstName { get; set; }

        public String LastName { get; set; }

        public int Age { get; set; }

        public DateTime JoinedDate { get; set; }


        public String EmailID { get; set; }

        public String ZIPCode { get; set; }

        public String Country { get; set; }

        public override string ToString()
        {
            return String.Format(
                "ProfileID={0},FirstName={1},LastName={2},Age={3},JoinedDate={4},Email={5},Zip={6},Country={7}",
                ProfileID,FirstName,LastName,Age,
                JoinedDate, EmailID,ZIPCode,Country
                );
        }
    }

}


Happy Coding ....

No comments:

Post a Comment