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