Bind DropDownList using simple user defined method in ASP.Net

Introduction:
In this article i will explain how to bind DropDownList using simple user defined method in ASP.Net.
Objective:
  1. Bind DropDownList using simple user defined method in ASP.Net
  2. User defined method should accept collection (e.g: List<Student> or List<Department> or List<User> or List<Role> etc)
  3. User defined method should accept DataTable as well.

In this article i created a class called Student.cs  as shown below:

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

public class Student
{
    public int StudentID { get; set; }
    public string StudentName { get; set; }

 
    public static List<Student> GetStudents()
    {
        var lstStudent = new List<Student>{
                new Student { StudentID = 1, StudentName = "Aditya Rao" },
                new Student { StudentID = 2, StudentName = "Utpal Lade" },
                new Student { StudentID = 3, StudentName = "Aditya Utpal" }
            };
        return lstStudent;
    }
}


I created a class called Deptment.cs  as shown below:

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

public class Deptment
{
    public int DeptmentID { get; set; }
    public string DeptmentName { get; set; }


    public static List<Deptment> GetDeptments()
    {
        var lstDeptment = new List<Deptment>{
                new Deptment { DeptmentID = 1, DeptmentName = "MCA" },
                new Deptment { DeptmentID = 2, DeptmentName = "MBA" },
                new Deptment { DeptmentID = 3, DeptmentName = "BCA" }
            };
        return lstDeptment;
    }
}

DropDownListDemo.aspx:-

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dopdownlistdemostaticdata.aspx.cs" Inherits="dopdownlistdemostaticdata" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
     Department  <asp:DropDownList ID="ddlDeptment" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlDeptment_SelectedIndexChanged"></asp:DropDownList><br/>

    Student<asp:DropDownList ID="ddlStudent" runat="server"></asp:DropDownList>
    </div>
    </form>
</body>
</html>


DropDownListDemo.aspx.cs:-


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

public partial class dopdownlistdemostaticdata : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            CommonUI.FillDropDown(ddlDeptment, Deptment.GetDeptments(), "DeptmentName", "DeptmentID");

        }
    }
    public class CommonUI
    {

        public static void FillDropDown<T>(DropDownList dropDownList, List<T> list, string DataTextField, string DataValueField)
        {
            if (list != null && list.Count > 0)
            {
                dropDownList.DataSource = list;
                dropDownList.DataTextField = DataTextField;
                dropDownList.DataValueField = DataValueField;
                dropDownList.DataBind();
            }
            // Insert 'Select' option at '0' index.
            dropDownList.Items.Insert(0, (new ListItem("Select", "-1")));
        }
    }
    protected void ddlDeptment_SelectedIndexChanged(object sender, EventArgs e)
    {
        int ddl = ddlDeptment.SelectedIndex;
        if (ddl == 1)
        {
            CommonUI.FillDropDown(ddlStudent, Student.GetStudents(), "StudentName", "StudentID");
        }

    }
 
}

FillDropDown method accepts 4 parameters:
  1. dropDownList - DropDownList ID for which you are binding
  2. list - It may be List<Student> or List<Department>, List<User>, List<Role>, etc.
  3. dataTextField - Gets or sets the field of the data source that provides the text content of the list items.
  4. dataValueField - Gets or sets the field of the data source that provides the value of each list item

Output:-




No comments:

Post a Comment

Xamarin Android Project App

Xamarin Android Project App 1. Xamarin -- Make a CALCULATOR Android App   https://drive.google.com/open?id=0B8OPCXLrtKPmWC1FWWtFM2lraVk...