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

Sort Dictionary Based On Value In Asp.Net And C#.Net | Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

In this tutorial i am going to explain about how to sort dictionary object based on value in asp.net and C#.Net or convert unsorted dictionary to sorted dictionary object in C#.Net and VB.Net or Convert Dictionary into KeyValuePair or KeyValuePair into Dictionary.

Code to create log files in C#.Net|Asp.Net

Introduction: In my previous article I have explained about how to create, delete and check whether the directory exists using C#.Net . In this article I am going to explain about  How to create log files in C#.Net. Explanation: Log files are useful to track any runtime errors and exceptions in all the applications. Below code will code will get the Message and Pagename as the input and creates the log file in that date. For that first i have imported below two namespaces.

Geckofx Browser in Winform Application

Bored with IE browser in your winform application ? Want to do everything as you doing in your Firefox or Chrome Browser ? Play with automation ? Then here is your choice . Introduction:  GeckoFX is a Windows Forms control written in clean, commented C# that embeds the Mozilla Gecko browser control in any Windows Forms Application. It also contains a simple class model providing access to the HTML and CSS DOM . GeckoFX was originally created by Andrew Young for the fast-growing visual CSS editor, Stylizer . It is now released as open-source under the Mozilla Public License.  You can download it here :   Geckofx 22.0 And the supporting file Xulrunner here :   Xulrunner Files Hope you have downloaded above two files. Here our journey going to start. Create your winform application in visual studio and do the following: right click the toolbox -> Choose items -> Browse the "Geckofx-winforms.dll" and click "yes" for “Load it anyw...