I’m trying to create some cascading drop downs. I have my States loading via C# page load fine. I just can’t seem to get anything to fire off to load the cities. After the cities, I’ll be adding another drop down, but I can figure that out once I have this working.
I did notice “Default.aspx/LoadCitiesByState 500 (Internal Server Error)” in my Chrome errors.
C#
protected void LoadStates()
{
// Populate State dropdown box
ListItem li = new ListItem();
li.Value = "0";
li.Text = "Select One";
DataSet dsState = SharedData.GetStates();
ddlState.DataSource = dsState.Tables[0].DefaultView;
ddlState.DataValueField = "ab";
ddlState.DataTextField = "name";
ddlState.DataBind();
ddlState.Items.Insert(0, li);
}
[WebMethod]
protected void LoadCitiesByState(string state)
{
ListItem li = new ListItem();
li.Value = "0";
li.Text = "Select One";
DataTable dt = new DataTable();
dt = SharedData.GetCities(state);
ddlCity.DataSource = dt.Rows[0].Table.DefaultView;
ddlCity.DataValueField = "ListCity";
ddlCity.DataTextField = "ListCity";
ddlCity.DataBind();
ddlCity.Items.Insert(0, li);
}
Markup
<asp:DropDownList ID="state" runat="server" MaxLength="50" Style="width: 200px;" />
<asp:DropDownList ID="city" runat="server" MaxLength="50" Style="width: 200px;" />
function loadCities(selectedItem) {
$.ajax({
type: "POST",
url: "Default.aspx/LoadCitiesByState",
data: "selectedItem",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: true,
success: function Success(data) {
alert(selectedItem);
}
});
I see you haven’t set
autopostbackto true inasp:DropDownList.