当前位置:  开发笔记 > 编程语言 > 正文

ASP.NET/C#:DropDownList在服务器控件中没有触发的SelectedIndexChanged

如何解决《ASP.NET/C#:DropDownList在服务器控件中没有触发的SelectedIndexChanged》经验,为你挑选了1个好方法。

我正在创建一个服务器控件,基本上绑定两个下拉列表,一个用于国家,一个用于州,并更新国家/地区的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 countries = 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在页面上,页面上的其他控件正确执行回发,而不是这个.



1> Chris Ballan..:

Viewstate是否已开启?

编辑: 也许您应该重新考虑覆盖渲染功能

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

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

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

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

推荐阅读
携手相约幸福
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有