Click here to Skip to main content
15,879,613 members
Articles / Programming Languages / Javascript

Cascaded dropdown using jQuery

Rate me:
Please Sign up or sign in to vote.
4.93/5 (12 votes)
28 May 2009CPOL2 min read 64.6K   1.4K   44  
Implementation of cascaded dropdowns using jQuery.
  • jquery.zip
    • Jquery
      • App_Code
      • App_Data
      • Bin
        • AjaxControlToolkit.dll
        • ar
          • AjaxControlToolkit.resources.dll
        • cs
          • AjaxControlToolkit.resources.dll
        • de
          • AjaxControlToolkit.resources.dll
        • es
          • AjaxControlToolkit.resources.dll
        • fr
          • AjaxControlToolkit.resources.dll
        • he
          • AjaxControlToolkit.resources.dll
        • hi
          • AjaxControlToolkit.resources.dll
        • it
          • AjaxControlToolkit.resources.dll
        • ja
          • AjaxControlToolkit.resources.dll
        • ko
          • AjaxControlToolkit.resources.dll
        • nl
          • AjaxControlToolkit.resources.dll
        • pt
          • AjaxControlToolkit.resources.dll
        • ru
          • AjaxControlToolkit.resources.dll
        • tr-TR
          • AjaxControlToolkit.resources.dll
        • zh-CHS
          • AjaxControlToolkit.resources.dll
        • zh-CHT
          • AjaxControlToolkit.resources.dll
      • Default.aspx
      • Default.aspx.cs
      • Default.js
      • jquery.validate.js
      • jquery-1.3.2.min.js
      • web.config
$(document).ready(function() 
    { 
        var Result;
       var Error;
       //var Parameters="{'x':'dave','y':'himanshu'}"; //Hardcode Parameters
       var xx='Himanshu1';
       var yy='Himanshu2';
       var Parameters="{'x':'"+xx+"','y':'"+yy+"'}"; //Dynamic Parameters
 $('.AJAX').click(function(e)
        { 
            CallAJAX(this.ServerMethod,Parameters,this.SuccessMethod,this.ErrorMethod,'#'+this.TargetControl,this.Item,this.Description);
            return false;
        }
    ); 
    
     $('.FillDD').change(function(e)
        { 
            if (parseInt($('#'+this.id).val())>0)
             {    
              var Parameters="{'Param':'"+document.getElementById(this.id).value+"'}";
              CallAJAX(this.ServerMethod,Parameters,this.SuccessMethod,this.ErrorMethod,'#'+this.TargetControl,this.Item,this.Description);
             }
             //   return false;
        }
    );
 
     function CallAJAX(ServerMethod,Parameters,SuccessMethod,ErrorMethod,ControlID,Item,Description)
     {
       $.ajax({
              type: "POST",
              url: ServerMethod,
              data:Parameters,
              contentType: "application/json; charset=utf-8",
              dataType: "json",
              error: function(xhr, status, errorThrown)
              {
                   Error=xhr;
                   eval(ErrorMethod + "()");
              },
              success: function(msg) 
              {
                   Result =msg;
                   var rr=  SuccessMethod + "('" + ControlID + "','" + Item + "','" + Description + "');";
                   eval(rr);
                   //eval(SuccessMethod + "()");//without parameter
              }
           });
      }
 
     function outputDT(ControlID,Item,Description)
     {
      BindDropdown(ControlID,Item,Description);
     }
      
      function BindDropdown(ControlID,Item,Description)
      {
            var opt='';
           opt += '<option value=-1>'+"---Select---"+'</option>';
            if(Result.d.Table.length>0) 
            {       
                for (var row in Result.d.Table)
                {
                
                 opt += '<option value="' + eval('Result.d.Table[row].'+Item)+ '">' + eval('Result.d.Table[row].'+Description) + '</option>';
                } 
            }
            else 
            { 
             opt ='<option>Sorry,No Record Found</option>'; 
            }  
           $(ControlID).html(opt); 
         
      }
      
        function ShowError()
        {
        alert(Error.responseText);
        }
      
     });

  

 
 

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
g

Comments and Discussions