我正在创建一个服务器控件,它基本上绑定了两个下拉列表,一个用于国家,一个用于州,并更新国家/地区的 selectedindexchanged 事件的状态下拉列表。但是,它不会发回。任何想法为什么?将它们包装在 UpdatePanel 中的奖励积分(存在渲染问题;可能是因为我没有要参考的页面?)
这是我所拥有的(去除了一些额外的数据访问内容):
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();
}
}
编辑:视图状态在页面上,页面上的其他控件正确执行回发,只是不是这个。