DropDownList with Custom Sorted List Binding using VB.NET
Step1) Create a ASP.NET Empty WebSite using VB.NET 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 Property ProfileID() As Integer
End Property
Public Property FirstName() As String
End Property
Public Property LastName() As String
End Property
Public Property Age() As Integer
End Property
Public Property JoinedDate() As DateTime
End Property
Public Property EmailID() As String
End Property
Public Property ZIPCode() As String
End Property
Public Property CounTry() As String
End Property
Public Overrides Function ToString() As String
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
)
End Function
End Class
Step 5) Build List of Sample Data for Profile List
Private Sub BuildProfile()
If profiles.Count = 0 Then
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)
}
)
End If
End Sub
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
Dim p As select
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.
Dim FirstName As [String] = DropDownList1.SelectedValue Dim profile As Profile = profiles.SingleOrDefault(Function(p) p.FirstName = FirstName) If profile IsNot Nothing Then Label1.Text = "<center>" & profile.ToString().Replace(",", "<br/>") & "</center>" End If
Step 11) Run the WebPage Default.aspx
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.vb
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Namespace WebApplication1
Public partial Class DropDownList_DisplayMember
Inherits System.Web.UI.Page
Private profiles As List<Profile> = New List<Profile>()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
BuildProfile()
If Not IsPostBack Then
var query = from p in profiles
orderby p.FirstName
Dim p As select
DropDownList1.DataSource = query
DropDownList1.DataTextField = "FirstName"
DropDownList1.DataBind()
DropDownList1.Items.Insert(0, "Select Profile")
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim FirstName As String = DropDownList1.SelectedValue
Dim profile As Profile = profiles.SingleOrDefault(p = > p.FirstName = FirstName)
If Not profile Is Nothing Then
Label1.Text = "<center>" + profile.ToString().Replace(",", "<br/>") + "</center>"
End If
End Sub
Private Sub BuildProfile()
If profiles.Count = 0 Then
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)
}
)
End If
End Sub
End Class
Public Class Profile
Public Property ProfileID() As Integer
End Property
Public Property FirstName() As String
End Property
Public Property LastName() As String
End Property
Public Property Age() As Integer
End Property
Public Property JoinedDate() As DateTime
End Property
Public Property EmailID() As String
End Property
Public Property ZIPCode() As String
End Property
Public Property CounTry() As String
End Property
Public Overrides Function ToString() As String
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
)
End Function
End Class
End Namespace
Happy Coding ....
No comments:
Post a Comment