Skip to main content

ASP.Net - Bind Array To DropDownList in C#.Net,VB.Net


 ASP.Net Code:

<!DOCTYPE html>
<html>
<head runat="server">
    <title>ASP.Net - Bind Array To Drop Down List in C#.Net,VB.NET</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="ddlArray" runat="server">
        </asp:DropDownList>
        <br />
        <asp:Button ID="btnFill" runat="server" Text="Fill Drop Down List From Array" OnClick="btnFill_Click" />
    </div>
    </form>
</body>
</html>

 C#.Net Code:

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

public partial class BindArrayToDropDown : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    // Button Click Event Handler
    protected void btnFill_Click(object sender, EventArgs e)
    {
        try
        {
            //Creating array object to Employee class and adding items to array
            Employee[] list = new Employee[]  {
                                 new Employee(1,"Kanna","Male","YP"),
                                 new Employee(2,"Dasan","Male","YP"),
                                 new Employee(3,"Ram","Male","YP"),
                                 new Employee(4,"Xavier","Male","YP"),
                                 new Employee(5,"Kaviyarasan","Male","YP"),
                                 new Employee(6,"Selvaraj","Male","YP"),
                                 new Employee(7,"Kumar","Male","YP")
                               };

            //Setting DataSource of DropDownList
            ddlArray.DataSource = list;
            //Setting DataTextField of DropDownList to EmployeeName field in the array
            ddlArray.DataTextField = "EmployeeName";
            //Setting DataValueField of DropDownList to Id field in the array
            ddlArray.DataValueField = "Id";
            //Binding drop down list
            ddlArray.DataBind();
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
   
}
public class Employee
{
    public int Id { get; set; }
    public string EmployeeName { get; set; }
    public string Gender { get; set; }
    public string Dept { get; set; }

    //Employee class constructor
    public Employee(int inId, string inEmployeeName, string inGender, string inDept)
    {
        Id = inId;
        EmployeeName = inEmployeeName;
        Gender = inGender;
        Dept = inDept;
    }
}


 VB.Net Code to bind Array to DropDown List
Imports System.Collections.Generic
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls

Partial Public Class BindArrayToDropDown
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)

    End Sub

    ' Button Click Event Handler
    Protected Sub btnFill_Click(sender As Object, e As EventArgs)
        Try
            'Creating array object to Employee class and adding items to array
            Dim list As Employee() = New Employee() {New Employee(1, "Kanna", "Male", "YP"), New Employee(2, "Dasan", "Male", "YP"), New Employee(3, "Ram", "Male", "YP"), New Employee(4, "Xavier", "Male", "YP"), New Employee(5, "Kaviyarasan", "Male", "YP"), New Employee(6, "Selvaraj", "Male", "YP"), _
             New Employee(7, "Kumar", "Male", "YP")}

            'Setting DataSource of DropDownList
            ddlArray.DataSource = list
            'Setting DataTextField of DropDownList to EmployeeName field in the array
            ddlArray.DataTextField = "EmployeeName"
            'Setting DataValueField of DropDownList to Id field in the array
            ddlArray.DataValueField = "Id"
            'Binding drop down list
            ddlArray.DataBind()
        Catch ex As Exception
            Response.Write(ex.Message)
        End Try
    End Sub

End Class
Public Class Employee
    Public Property Id() As Integer
        Get
            Return m_Id
        End Get
        Set(value As Integer)
            m_Id = Value
        End Set
    End Property
    Private m_Id As Integer
    Public Property EmployeeName() As String
        Get
            Return m_EmployeeName
        End Get
        Set(value As String)
            m_EmployeeName = Value
        End Set
    End Property
    Private m_EmployeeName As String
    Public Property Gender() As String
        Get
            Return m_Gender
        End Get
        Set(value As String)
            m_Gender = Value
        End Set
    End Property
    Private m_Gender As String
    Public Property Dept() As String
        Get
            Return m_Dept
        End Get
        Set(value As String)
            m_Dept = Value
        End Set
    End Property
    Private m_Dept As String

    'Employee class constructor
    Public Sub New(inId As Integer, inEmployeeName As String, inGender As String, inDept As String)
        Id = inId
        EmployeeName = inEmployeeName
        Gender = inGender
        Dept = inDept
    End Sub
End Class



Comments

Popular posts from this blog

Code To Convert rupees(numbers) into words using C#.Net

Introduction: In my previous article I have explained about how to validate emailid using javascript . In this article I am going to explain about code used to convert rupees(numbers) into words using C#.Net . Explanation: For explanation purpose I have a page. It has a textbox to input the numbers. And when you click on the convert to words button then it will convert the input numbers into words and shows it in the below label. Below is the C# code used to do this functionality. public static string NumbersToWords( int inputNumber) {     int inputNo = inputNumber;     if (inputNo == 0)         return "Zero" ;     int [] numbers = new int [4];     int first = 0;     int u, h, t;     System.Text. StringBuilder sb = new System.Text. StringBuilder ();     if (inputNo < 0)     { ...

Puzzles for kids - 12 Days Of Christmas

According to the traditional song, on the first day of Christmas (25th December), my true love sent to me: . A partridge in a pair tree On the second day of Christmas (26th December), my true love sent to me THREE presents: . Two turtle doves . A partridge in a pear tree On the third day of Christmas (27th December and so on) my true love sent to me SIX presents: . Three French hens . Two turtle doves . A partridge in a pear tree This carries on until the the twelfth day of Christmas, when my true love sends me: Twelve drummers drumming Eleven pipers piping Ten lords a-leaping Nine ladies dancing Eight maids a-milking Seven swans a-swimming Six geese a-laying Five gold rings Four calling birds Three French hens Two turtle doves A partridge in a pear tree After the twelve days of Christmas are over, how many presents has my true love sent me altogether? Our Solution: 1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55 + 66 + 78 = 364 presents Which is really interesting when you think ...

Asp.Net DataTable Manipulation - Add, Update,Delete & Sort DataTable in C#.Net & VB.Net

In the asp.net developer's life it is very common to come across the datatable manipulation. So here i have decided to explain the datatable manipulation like adding records to datatable, editing and updating datatable records,deleting records from datatable based on the condition and finally sorting datatable based on columns.