Problem in populating DropDownList using Webservice through AJAX call.

Last post 07-07-2009 11:24 PM by Vince Xu - MSFT. 1 replies.

Sort Posts:

  • Problem in populating DropDownList using Webservice through AJAX call.

    07-06-2009, 6:29 AM
    • Member
      point Member
    • Nikhil Uppaluri
    • Member since 06-18-2009, 2:07 PM
    • Bangalore
    • Posts 3

    Hi All,

    Im populating a DropDownList based on selection of another DropDowList.

    Here is my code :

    <script language="javascript" type="text/javascript">
    $(document).ready(function() {
            InitJQuery();    
        });
        
        // this function will be called after the UpdatePanel finishes processing and set up jQuery again
        function pageLoad() {
            InitJQuery();
        }
        function InitJQuery() 
        {
          
            $(".ParentList").bind("change", function() 
            { 
                var selectedValue = $(this).val();           
                var ElementId = this.id; 
                var first = ElementId.lastIndexOf('_');                                
                var second = ElementId.substring(first,0);           
                var last = ElementId.substring(ElementId.length,first);
                var PrimaryRoleId = last.replace("_","");              
                var SubCategoryId = second + "_sub_" + PrimaryRoleId;  
                var select = $("#" + SubCategoryId);
                          
                select.append("<option>please wait..</option>");
                
                $.ajax({                
                    type: "POST",
                    url: "http://<%=PortalAlias.HTTPAlias%>/Services/ACWebSvc.asmx/getChildData",
                    data: "{'id':'" + PrimaryRoleId + "','selectedValue':'" + selectedValue + "'}",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    success: function(response) 
                    {
                        var dataList = (typeof response.d) == 'string' ? eval('(' + response.d + ')') : response.d;
                        select.empty(); 
                          
                        if(dataList.length == 0)
                        {
                            select.append("<option class='ddheader'value='0'>-N/A-</option>");
                        }
                        else
                        { 
                                                   
                            for (var i = 0; i < dataList.length; i++) 
                            {
                                select.append("<option class='ddheader'>" + dataList[i].ListName + "</option>");
                            } 
                        }                   
                    },
                    error: function(xhr) 
                    {
                        alert(xhr.statusText);
                    }
                });
            });
         }    
    </script>
    
    <tr>
    <td>
    <asp:DropDownList ID="acListPrimaryRole" runat="server"  CssClass="ParentList" Width="150px">
        </asp:DropDownList>
    </td>
    <td>
    <asp:DropDownList ID="acListPrimaryCategory" runat="server" CssClass="ParentList" Width="150px">
        </asp:DropDownList>
    </td>
    </tr>
    
    <tr>
    <td>
    <asp:DropDownList ID="sub_acListPrimaryRole" CssClass="ChildList" runat="server" Width="150px">
           <asp:ListItem Text="-Select Sub Role-" Value="0" Selected="True"></asp:ListItem>
        </asp:DropDownList>
    </td>
    <td>
        <asp:DropDownList ID="sub_acListPrimaryCategory" CssClass="ChildList" runat="server" Width="150px">
           <asp:ListItem Text="-Select Sub Category-" Value="0" Selected="True"></asp:ListItem>
        </asp:DropDownList>
    </td>
    </tr>
    
    
    My WebService:
    
    <System.Web.Script.Services.ScriptService()> _
    <WebService(Namespace:="http://tempuri.org/")> _
    <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
    Public Class ACWebSvc
        'Inherits System.Web.Services.WebService
        Inherits Entities.Modules.PortalModuleBase
        Dim _prfController As ProfileController
    
    <WebMethod()> _
        Public Function getChildData(ByVal id As String, ByVal selectedValue As String) As List(Of dataList)
            Dim _prfController As ProfileController = New ProfileController
            Dim dsChild As DataSet = _prfController.Profile_GetSecondaryList(selectedValue, id)
            Dim l As New Generic.List(Of dataList)
            Dim e As New dataList("0", "Select")
            For Each row As DataRow In dsChild.Tables(0).Rows
                e = New dataList(row("ListId"), row("ListName"))
                l.Add(e)
            Next
            Return l
        End Function
    
    Im Binding data to first dropdown list statically.

    Its working fine , im able to bind the data to second dropdownlist on selection of firstdropdown list ,

    This is for some search functionality ,on click if button on this page im populating some results ,But problem is im losing the selection(second list) after postback .

    How to get that selection on post back ?

  • Re: Problem in populating DropDownList using Webservice through AJAX call.

    07-07-2009, 11:24 PM
    Answer

    Hi,

    When you change the selection of DropDownList, please save the selectedIndex into a hiddencontrol so that it can get the selectedIndex on serverside in button click.


    Vince Xu
    Microsoft Online Community Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Page 1 of 1 (2 items)