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

Binding dropdowns using Ajax

Rate me:
Please Sign up or sign in to vote.
2.71/5 (4 votes)
25 May 20071 min read 54.7K   621   20  
How to populate a dropdown in another dropdown change
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="~/SampleforDatabind.aspx.cs" Inherits="MyAjaxSample.SampleforDatabind" %>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Sample Page for bind the data using Ajax</title>
</head>
<body>
    <form id="form1" runat="server" method="post">
    <script language=javascript>
    function BindDatatoTable()
    {
        MyAjaxSample.SampleforDatabind.GetDataSet(BindDatatoTable_callback);
    }
    function BindDatatoTable_callback(responce)
    {
        if(responce.error == null) 
        { 
        var ds = responce.value;
        if(ds!= null && typeof(ds) == "object" && ds.Tables!=null)
            {
            var s = new Array();
            s[s.length] = "<table border = 1 cellpadding=0 cellspacing=0 bordercolor=blue >";
                for(var i=0;i<ds.Tables[0].Rows.length;i++)
                    {
                    s[s.length] = "<tr>";
                    s[s.length] = "<td>" + ds.Tables[0].Rows[i].TabID + "</td>";
                    s[s.length] = "<td>" + ds.Tables[0].Rows[i].TabName + "</td>";
                    s[s.length] = "<td>" + ds.Tables[0].Rows[i].Remarks + "</td>";
                    s[s.length] = "</tr>";
                    }
            s[s.length] = "</table>";
            document.getElementById("Display1").innerHTML = s.join("");
           } 
        } 
     }
       function GetTabSet()
        {
         var countryId = document.getElementById("ddlList").value;// ddlList.options[ddlList.selectedIndex].value;
         MyAjaxSample.SampleforDatabind.GetTabSet(countryId, GetTabSet_CallBack);
        }

       function GetTabSet_CallBack(response)
        {
             if (response.error != null)
             {
              alert(response.error); 
              return;
             }

         var states = response.value;  
             if (states == null || typeof(states) != "object")
             {
              return;
             }
         var statesList = document.getElementById("ddlItemList");
			 statesList.options.length = 0; 
			 
			
             for (var i = 0; i < response.value.Tables[0].Rows.length; ++i)
             {
				 statesList.options[statesList.options.length] = new Option(response.value.Tables[0].Rows[i].TabName,response.value.Tables[0].Rows[i].TabID);
				//statesList.options[statesList.options.length] = "<option>" + response.value.Tables[0].Rows[i].TabName + "</option>";
				//document.getElementById("ddlItemList").innerHTML = "<select id=\"sel\">" + html.join("") + "</select>";
             }
        }
    </script>
    <div>
		
        <input id="btnLoad" type="button" value="Load Data" onclick="BindDatatoTable();" /></div>
        <span id="Display1" title="Loading the Data from the Database using Ajax">Loading the Data from the Database using Ajax</span>
        <br /><p>Menu</p>
         <asp:DropDownList ID="ddlList" onchange="GetTabSet()" runat="server"></asp:DropDownList>
        <asp:DropDownList ID="ddlItemList" runat="server" ></asp:DropDownList>
        <br />
    </form>
</body>
</html>

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


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