Advertise with us

How to Use DropDownList Dynamically Using Ajax

Posted by  Ankit Singh
 1201  View(s)
Rate this:
I want to use DropDownList Dynamically Using Ajax in please help me
  1. Re: How to Use DropDownList Dynamically Using Ajax

    HTML dropdown list statically is very easy. It is done directly using a “select” tag , but when we have a requirement to add the data in HTML dropdownlist dynamically then the things change. At that time we must load the data from the database.

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UsingAjaxDropDown.aspx.cs" Inherits="Forumasp.UsingAjaxDropDown" %>

    <!DOCTYPE html>

    <html xmlns="">
    <head runat="server">
         <script src="Scripts/jquery-2.1.4.js"></script>
             $(function () {
                     type: "POST",                
                     url: "UsingAjaxDropDown.aspx/GetData",
                     data: {},
                     contentType: "application/json; charset=utf-8",
                     dataType: "json",
                     asnyc: false,               
                     success: function (data) {
                         var result = data['d'];
                         for (var i = 0; i < result.length; i++) {
                             var opt = new Option(result[i].Emp_Name);

        <form id="form1" runat="server">
                <label>Select Employee :</label> 
                <select class="form-control" id="opemp" style="width:200px;"> 
                    <option selected="selected">Select</option> 

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace Forumasp
        public partial class UsingAjaxDropDown : System.Web.UI.Page
            protected void Page_Load(object sender, EventArgs e)


            public static List<Employee> GetData()
                List<Employee> Emp = new List<Employee>();
                DataTable dt = new DataTable();
                string constr = ConfigurationManager.ConnectionStrings["forumConnectionString"].ToString(); // connection string
                SqlConnection con = new SqlConnection(constr);
                SqlCommand com = new SqlCommand("select *from Employee", con); // table name
                SqlDataAdapter da = new SqlDataAdapter(com);
                DataSet ds = new DataSet();
                dt = ds.Tables[0];
                Emp = (from DataRow row in dt.Rows

                       select new Employee
                           Emp_ID = Convert.ToInt32(row["Emp_ID"]),
                           Emp_Name = row["Emp_Name"].ToString(),
                           Emp_Sal =Convert.ToDecimal( row["Emp_Sal"])

                return Emp;

        public class Employee
            public int Emp_ID { get; set; }
            public string Emp_Name { get; set; }
            public decimal Emp_Sal { get; set; }



      Modified On Apr-10-2018 06:11:49 AM