Click here to Skip to main content
14,332,423 members

Adding values to a dropdown using AJAX

Rate this:
1.93 (5 votes)
Please Sign up or sign in to vote.
1.93 (5 votes)
6 Jun 2007CPOL
How to add values to a dropdown using AJAX.

Screenshot - Image.jpg


This article will describe how to add items to a dropdown using AJAX.


I have used AjaxPro2.dll here and C# as the language. In the Value text box, type the value and press the Save button. The value will be saved in a SQL Server table and then retrieved from it and populated in the dropdown with out postback.

Using the code

Download the source code from the link above. Create a table in SQL Server named testing with the following fields: ID, Name. ID as identity Int, and Name varchar(50).

In the web.config file, inlude the name of the database in which you created the table named testing.

Run the application.

// In Code behind add the code like this
namespace MyAjaxSample
public partial class Testing :System.Web.UI.Page 
    public static string connectionString = 
    public DataSet ds = new DataSet(); 
    DBClass MyClass = new DBClass(); 
    public SqlDataAdapter ObjSDA; 
    public SqlCommand sc; 
    protected void Page_Load(object sender, EventArgs e) 
        if (!IsPostBack) 
            SqlConnection myConnection = new SqlConnection(connectionString); 
            ObjSDA = new SqlDataAdapter("Select * from Testing", myConnection);
            DropDownList1.DataSource = ds; 
            DropDownList1.DataTextField = "Name"; 
            DropDownList1.DataValueField = "ID"; 
            DropDownList1.Items.Add("--Select table--");
            DropDownList1.SelectedIndex = DropDownList1.Items.Count - 1; 
    public DataSet Inserting(string Name) 
        SqlConnection myConnection = new SqlConnection(connectionString); 
        string strQuery = "Insert into Testing Values('" + Name + "')"; 
        int res = MyClass.ExecuteQry(strQuery); 
        ObjSDA = new SqlDataAdapter("Select * from Testing", myConnection);
        return ds; 

In the script side, you add the following code:

<script language="javascript">
function Inserting()
    if(document.getElementById("TextBox2").value == "")
        alert('Enter the name'); 
        var name = document.getElementById("TextBox2").value;

function InsertData_callback(response)
    if (response != null && response.value != null)
        var List = document.getElementById("DropDownList1");
        List.options.length = 0; 
        for (var i = 0; i < response.value.Tables[0].Rows.length; ++i)
            List.options[List.options.length] = 
              new Option(response.value.Tables[0].Rows[i].Name,
        document.getElementById("TextBox2").value ="";

Here is the database class:

namespace Ganesh
public class DBClass
    SqlConnection ObjCon = 
      new SqlConnection(
    public SqlCommand sc;
    public SqlDataAdapter sda;
    public SqlDataReader sdr;
    public DataSet ds;
    public int res;

    public DBClass()
    // TODO: Add constructor logic here

    public int ExecuteQry(string qry)
        if (ObjCon.State == ConnectionState.Open) ObjCon.Close();
        sc = new SqlCommand(qry, ObjCon);
        int res = sc.ExecuteNonQuery();
        return res;

Try this and let me know me your suggestions. Thanks.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Ganesan Sankaran
Web Developer
India India
I'm Ganesan.S,
Software Engineer
Involved in developing MS applications for last 7 Yrs in VB,VB.NET,ASP.NET,Java Script and C#.NET lately into EPiServer and Ajax.

Comments and Discussions

QuestionJavaScript ?? Pin
mag136-Jun-07 22:27
membermag136-Jun-07 22:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Posted 6 Jun 2007


27 bookmarked