c# - ASP.NET/C# : DropDownList SelectedIndexChanged in server control not firing

标签 c# asp.net drop-down-menu custom-server-controls selectedindexchanged

我正在创建一个基本上绑定(bind)两个下拉列表的服务器控件,一个用于国家/地区,一个用于州,并在国家/地区的 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();
    }
}

编辑:Viewstate在页面上,页面上的其他控件正确执行回发,只是不是这个。

最佳答案

Viewstate 是否开启?

编辑: 也许你应该重新考虑重写渲染函数

  protected override void RenderContents(HtmlTextWriter output)
    {
        ddlCountries.RenderControl(output);
        ddlStates.RenderControl(output);
    }

而是将下拉列表添加到控件并使用默认 RenderContents 呈现控件。

编辑: 请参阅我在之前的评论中提到的丹尼斯的回答:

Controls.Add ( ddlCountries );
Controls.Add ( ddlStates );

关于c# - ASP.NET/C# : DropDownList SelectedIndexChanged in server control not firing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/570294/

相关文章:

asp.net - WinDBG - 不包括符号路径

jquery - 单击两个下拉菜单

c# - XML架构

asp.net - Uploadify for ASP.NET 应用程序为大文件返回 HTTP 错误

c# - 在 linq 查询中设置和使用值

c# - 单击下拉列表的箭头/延迟加载下拉列表加载下拉列表

internet-explorer - 使用 VBA Excel 通过更改下拉值来导航网站

android - 如何在单击按钮时创建下拉菜单

c# - 在 ASP.NET MVC 项目中使用 Serilog

c# - 使用 C# 给定 hwnd 在 Chrome、Firefox 和 IE 中获取当前 URL