Hello, I cannot find the problem with my code to be able to populate a dropdownlist based upon a previous dropdownlist selection.

Here is all the necessary code

HTML

<div class="control">
    		<asp:DropDownList ID="ddlEstado" 
                CssClass="ui-state-default ui-corner-all filterDropDown" runat="server" 
                DataSourceID="sdsEstadoDDL" DataTextField="nombre" DataValueField="nombre">
            </asp:DropDownList>
    	    <asp:SqlDataSource ID="sdsEstadoDDL" runat="server" 
                ConnectionString="<%$ ConnectionStrings:QMPConnectionString %>" 
                SelectCommand="EXEC Catalogs.sp_ObtenerEstado"></asp:SqlDataSource>
    	</div>    
        <div>
    		<asp:DropDownList ID="ddlMunicipio" runat="server" ></asp:DropDownList>
    	</div>

Javascript

$(function () {
    $('[id$="ddlMunicipio"]').attr('disabled', true);
    $('[id$="ddlEstado"]').change(obtenerMunicipios);   
});

function obtenerMunicipios() {
    $.ajax({
        type: "POST",
        url: "WebService.asmx/ObtenerMunicipios",
        data: '{sEstado: Aguascalientes}',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (response) { recibirMunicipios(response.d); },
        error: function () { alert("Error"); }
    });
}

function recibirMunicipios(response) {
    popularMunicipios(response.d);
}

function popularMunicipios(alMunicipios) {
    $('[id$="ddlMunicipio"]').removeAttr("disabled");
    $.each(alMunicipios, function () {
        $('[id$="ddlMunicipio"]').append($("<option></option>").val(this['Value']).html(this['Value']));
    });
}

SERVER SIDE

using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Collections;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Web.Script.Services;

/// <summary>
/// 
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[ScriptService]
public class WebService : System.Web.Services.WebService {
    
    public WebService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public static ArrayList ObtenerMunicipios(string sEstado)
    {
        string sConnString = ConfigurationManager.ConnectionStrings["QMPConnectionString"].ConnectionString;
        ArrayList alMunicipios = new ArrayList();

        using (SqlConnection scConexion = new SqlConnection(sConnString))
        {
            using (SqlCommand scComando = new SqlCommand("Catalogs.sp_ObtenerMunicipios", scConexion))
            {
                scComando.CommandType = CommandType.StoredProcedure;
                scComando.Parameters.AddWithValue("@estado", sEstado);
                scConexion.Open();
                SqlDataReader sdr = scComando.ExecuteReader();
                while (sdr.Read())
                {
                    alMunicipios.Add((string)sdr["nombre"]);
                }
                scConexion.Close();
                return alMunicipios;
            }
        }
    }
    
}

This is my first time using WebServices Json etc. so I'm not very experienced but based on my research everything seems fine

Thanks in advance

Hi, it seams that it never arrives to the web method, but I don't know why the default.aspx file is in the same directory as the WebService.asmx file, the .cs of the WebService.asmx is in the App_Code folder though.

If I put a debugg mark inside my web methods and then test the page it never arrives there so I know I'm not even getting there, still I have tested the web method by it self and it works.

What can I do I have been trying to fix this for a lot of time now and nothing seems to work.

THanks in advance

Just an update, I just realize the error is an internet 500 server error and using Fiddler I got the Unknown Web Method error.

The problem is that I have the [WebMethod] attribute above my static method and in fact I can access the method fine with server side code, the problem is with jquery it does not find the method at all.

Please help me,

Thanks in advance

Finally, I had to remove the static from the methods because I am using a Webservice and the examples I was looking where aspx pages...

simple solution but it took me a while to find it, hope this might help someone.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.