Introduction

In this article, I will discuss how to bind a select option on drop-down using pure JavaScript. Client-side binding is faster than that of the server-side. So, I need to bind the select option or drop-down using JavaScript.
Example
To achieve my goal, I will present a demo here. In the below demo, I will bind the student names in the dropdown.
Client-side code
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="BindDropDown.aspx.cs" Inherits="XMLWrite.BindDropDown" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title></title>
  6. <script type="text/javascript">
  7. var ddlStudents;
  8. function GetStudentDetails() {
  9. ddlStudents = document.getElementById("<%=ddlStudents.ClientID %>");
  10. PageMethods.GetStudentDetails(OnSuccess);
  11. }
  12. window.onload = GetStudentDetails;
  13. function OnSuccess(response) {
  14. ddlStudents.options.length = 0;
  15. AddOption("Please select", "0");
  16. for (var i in response) {
  17. AddOption(response[i].Name, response[i].Id);
  18. }
  19. }
  20. function AddOption(text, value) {
  21. var option = document.createElement('option');
  22. option.value = value;
  23. option.innerHTML = text;
  24. ddlStudents.options.add(option);
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <form id="form1" runat="server">
  30. <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
  31. </asp:ScriptManager>
  32. <div>
  33. <asp:DropDownList ID="ddlStudents" runat="server">
  34. </asp:DropDownList>
  35. </div>
  36. </form>
  37. </body>
  38. </html>
Server-side code
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Script.Services;
  6. using System.Web.Services;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. namespace XMLWrite
  10. {
  11. public partial class BindDropDown : System.Web.UI.Page
  12. {
  13. protected void Page_Load(object sender, EventArgs e)
  14. { }
  15. public class StudentDetails
  16. {
  17. public int Id { get; set; }
  18. public string Name { get; set; }
  19. }
  20. [WebMethod]
  21. public static List<StudentDetails> GetStudentDetails()
  22. {
  23. List<StudentDetails> lstStudentDetails = new List<StudentDetails>();
  24. StudentDetails studentDetails = new StudentDetails();
  25. studentDetails.Id = 1;
  26. studentDetails.Name = "Pradosh";
  27. lstStudentDetails.Add(studentDetails);
  28. studentDetails = new StudentDetails();
  29. studentDetails.Id = 2;
  30. studentDetails.Name = "Bibhu";
  31. lstStudentDetails.Add(studentDetails);
  32. studentDetails = new StudentDetails();
  33. studentDetails.Id = 3;
  34. studentDetails.Name = "Niladri";
  35. lstStudentDetails.Add(studentDetails);
  36. studentDetails = new StudentDetails();
  37. studentDetails.Id = 4;
  38. studentDetails.Name = "Brahma";
  39. lstStudentDetails.Add(studentDetails);
  40. return lstStudentDetails;
  41. }
  42. }
  43. }

Summary

I hope the above example will help you to bind the dropdown using JavaScript.