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

By
Advertisement

 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



0 comments:

Post a Comment

Online Casino