Sto creando un controllo server che lega essenzialmente due elenchi a discesa, uno per paese e uno per stato, e aggiorna il menu a discesa stato sull'evento selezionatoindexchanged. Tuttavia, non sta postando indietro. Qualche idea, perché? I punti bonus per avvolgendoli in un UpdatePanel (avendo problemi di rendering, forse perché io non ho una pagina di riferimento?)ASP.NET/C#: DropDownList SelectedIndexChanged nel controllo del server non attivo
Ecco quello che ho (con alcune cose l'accesso ai dati in più spogliato fuori):
public class StateProv : WebControl
{
public string SelectedCountry;
public string SelectedState;
private DropDownList ddlCountries = new DropDownList();
private DropDownList ddlStates = new DropDownList();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
IList<Country> countries = GetCountryList();
IList<State> states = new List<State>();
if (SelectedCountry != null && SelectedCountry != "")
{
states = GetStateList(GetCountryByShortName(SelectedCountry).CountryShortName);
}
else
{
states.Add(new State { CountryId = 0, Id = 0, StateLabelName = "No states available", StateLongName = "No states available", StateShortName = "" });
}
ddlCountries.DataSource = countries;
ddlCountries.DataTextField = "CountryLongName";
ddlCountries.DataValueField = "CountryShortName";
ddlCountries.SelectedIndexChanged += new EventHandler(ddlCountry_SelectedIndexChanged);
ddlCountries.AutoPostBack = true;
ddlStates.DataSource = states;
ddlStates.DataTextField = "StateLongName";
ddlStates.DataTextField = "StateShortName";
ddlCountries.DataBind();
ddlStates.DataBind();
if (!string.IsNullOrEmpty(SelectedCountry))
{
ddlCountries.SelectedValue = SelectedCountry;
if (!string.IsNullOrEmpty(SelectedState))
{
ddlStates.SelectedValue = SelectedState;
}
}
}
protected override void RenderContents(HtmlTextWriter output)
{
ddlCountries.RenderControl(output);
ddlStates.RenderControl(output);
}
private IList<Country> GetCountryList()
{
//return stuff
}
private IList<State> GetStateList(Country country)
{
//return stuff
}
private IList<State> GetStateList(string countryAbbrev)
{
Country country = GetCountryByShortName(countryAbbrev);
return GetStateList(country);
}
private Country GetCountryByShortName(string countryAbbrev)
{
IList<Country> list = dataAccess.RetrieveQuery<Country>();
//return stuff
}
private IList<State> GetAllStates()
{
//return stuff
}
protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
{
IList<State> states = GetStateList(GetCountryList()[((DropDownList)sender).SelectedIndex]);
ddlStates.DataSource = states;
ddlStates.DataBind();
}
}
Modifica: Viewstate si trova sulla pagina e altri controlli sulla pagina eseguono correttamente i postback, ma non questo.
Probabilmente non conta come una risposta, ma l'Ajax Control Toolkit offre quello che vuoi già all'interno di un pannello di aggiornamento: http://www.asp.net/AJAX/AjaxControlToolkit/Samples/CascadingDropDown/CascadingDropDown.aspx – Sean
Sì; Ho giurato per sempre l'ACT per sempre. Immondizia assoluta IMO; In realtà sto costruendo un sostituto per il mio progetto, dal momento che solo il controllo CCD può funzionare con un servizio Web, con altre implicazioni. –