Hello, someone could help me. I have the following problem I have a TextBox and a DropDownList, in the textbox has an autocomplete with names of states; after finishing fill in the textbox populates DropDownList with names of cities corresponding to that state. everything worked correctly. the difficulty I came across was to retrieve the city code in the Button event
follow 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 of the buttom "Unnamed1_Click" I made tests of the whole form but it always comes empty with no selected item, and when I create a javascript function with an alert it picks up the perfect id, is that in this situation it picks up only via javascript anyway? Below is an example of the test alert in which I'm picking up the city id in DropDownList
function myFunction() {
var DropdownList = document.getElementById('<%=dpdCidades.ClientID %>');
//var SelectedIndex = DropdownList.selectedIndex;
var SelectedValue = DropdownList.value;
alert(SelectedValue);
}