Click here to Skip to main content
15,895,777 members
Articles / Web Development / ASP.NET

Adding values to a dropdown using AJAX

Rate me:
Please Sign up or sign in to vote.
1.93/5 (5 votes)
6 Jun 2007CPOL 42.5K   27  
How to add values to a dropdown using AJAX.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Ganesh;

namespace MyAjaxSample
    {
    public partial class Testing : System.Web.UI.Page
        {
        public static string connectionString = (string)ConfigurationManager.AppSettings["ConnectionString"];
        public DataSet ds = new DataSet();
        DBClass MyClass = new DBClass();
        public SqlDataAdapter ObjSDA;
        protected void Page_Load(object sender, EventArgs e)
            {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(Testing));
            if (!IsPostBack)
                {
                DropDownList1.DataSource = MyClass.GetDataSet("Select * from Testing");
                DropDownList1.DataTextField = "Name";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();
                DropDownList1.Items.Add("--Select table--");
                DropDownList1.SelectedIndex = DropDownList1.Items.Count - 1;
                }
            }
        [AjaxPro.AjaxMethod]
        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);
            ObjSDA.Fill(ds);
            return ds;
            }
        }
    }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
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