我正在创建一个服务器控件,基本上绑定两个下拉列表,一个用于国家,一个用于州,并更新国家/地区的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); IListcountries = GetCountryList(); IList states = new List (); 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 GetCountryList() { //return stuff } private IList GetStateList(Country country) { //return stuff } private IList GetStateList(string countryAbbrev) { Country country = GetCountryByShortName(countryAbbrev); return GetStateList(country); } private Country GetCountryByShortName(string countryAbbrev) { IList list = dataAccess.RetrieveQuery (); //return stuff } private IList GetAllStates() { //return stuff } protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e) { IList states = GetStateList(GetCountryList()[((DropDownList)sender).SelectedIndex]); ddlStates.DataSource = states; ddlStates.DataBind(); } }
编辑: Viewstate在页面上,页面上的其他控件正确执行回发,而不是这个.
Viewstate是否已开启?
编辑: 也许您应该重新考虑覆盖渲染功能
protected override void RenderContents(HtmlTextWriter output) { ddlCountries.RenderControl(output); ddlStates.RenderControl(output); }
而是将下拉列表添加到控件并使用默认的RenderContents呈现控件.
编辑: 请参阅丹尼斯的答案,我在之前的评论中提到过:
Controls.Add ( ddlCountries ); Controls.Add ( ddlStates );