1
Someone could help me. I have the following problem I have a Textbox and a Dropdownlist, in the textbox there is an autocomplete with state names; after finishing the textbox fills Dropdownlist with city names corresponding to that state. until then everything works properly. the difficulty I encountered was in recovering the city code at the Button event
follows the code below
javascript
$(document).ready(function () {
populaDropDownList();
volume();
});
function populaDropDownList() {
$.ajax({
type: "POST",
url: "Default.aspx/getDados",
data: "{'CountryName':'" + $("#txtEstado").val() + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var Dropdown = $('#<%=dpdCidades.ClientID%>');
Dropdown.empty();
//$(Dropdown).empty();
Dropdown.append(new Option("Selecione", 0));
$.each(response.d, function (index, item) {
Dropdown.append(new Option(item.Nome, item.Id));
});
},
error: function () {
alert("Falha ao carregar dados");
}
});
}
function volume() {
$("#txtEstado").autocomplete({
//DropDownList
change: function () {
populaDropDownList();
},
//AutoComplete
source: function (request, response) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: "Default.aspx/getEstado",
data: "{'CountryName':'" + $("#txtEstado").val() + "'}",
dataType: "json",
success: function (data) {
response(data.d);
},
error: function (result) {
alert("Error");
}
});
}
});
}
//************** Default.aspx //*****************
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<p>
<asp:TextBox runat="server" ID="txtEstado" Width="120">
</asp:TextBox>
</p>
<p>
<asp:DropDownList runat="server" ID="dpdCidades" Width="120"></asp:DropDownList>
<asp:Label Text="" ID="msg" runat="server" />
</p>
<p>
<asp:Button Text="Pesquisar" runat="server" ID="lblPesquisar" OnClick="Unnamed1_Click" />
</p>
</div>
Default.aspxcs
[WebMethod]
public static List<Cidade> getDados(string CountryName)
{
var con = new Conexao();
var estado = new Estado();
estado.Nome = CountryName;
var lst = con.ListatodasCidades(estado).ToList();
return lst.ToList();
}
[WebMethod]
public static List<string> getEstado(string CountryName)
{
var con = new Conexao();
var estado = new Estado();
var strLstEstado = new List<string>();
estado.Nome = CountryName;
var lst = con.ListaEstados(estado).ToList();
foreach (var item in lst)
{
strLstEstado.Add(item.Nome);
}
return strLstEstado.ToList();
}
protected void Unnamed1_Click(object sender, EventArgs e)
{
var teste1 = dpdCidades.SelectedValue;
var teste12 = dpdCidades.SelectedItem;
var teste13 = dpdCidades.Text;
var s4 = string.IsNullOrEmpty(dpdCidades.Text) ? 0 : Convert.ToInt32(dpdCidades.Text);
PopularGrid();
}
Note that in the event buttom "Unnamed1_click" I did tests all the way but always comes empty with no selected item, and when I create a javascript function with an Alert it picks the perfect id, is it in this situation it only takes javascript anyway? Just below is an example of the test Alert in which I am getting the perfect city id on the Dropdownlist
function myFunction() {
var DropdownList = document.getElementById('<%=dpdCidades.ClientID %>');
//var SelectedIndex = DropdownList.selectedIndex;
var SelectedValue = DropdownList.value;
alert(SelectedValue);
}