相应地动态创建下拉列表和触发事件

时间:2013-09-04 15:08:52

标签: c# asp.net

我有一个简单的任务。第一个Dropdownlist控件是加载国家/地区名称的位置。选择国家名称后,动态Dropdownlist将加载相应的状态,选择状态后,动态地将另一个Dropdownlist添加到相关区域。问题是不会触发动态选择索引事件。我搜索了很多页面,但找不到合适的答案。任何人都可以回答它的书面代码。

此代码在静态控件中运行良好。但不是动态控制。 任何人都可以更正我的代码

namespace fireProgram
 {
    public partial class MindforeSystemTestingProgram : System.Web.UI.Page
    {
      BALayer objBALayer = new BALayer();
      DropDownList ddlState=new DropDownList();
      DropDownList ddlDistrict=new DropDownList();
    protected void Page_Init(EventArgs e)
    {
        ddlState.ID = "ddlState";
        ddlState.AutoPostBack = true;
        ddlState.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged);
        panel1.Controls.AddAt(2, ddlState);


        ddlDistrict.ID = "ddlDistrict";
        panel1.Controls.AddAt(3, ddlDistrict);      
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ddlCountry.DataSource = objBALayer.GetCountry();
            ddlCountry.DataTextField = "Text";
            ddlCountry.DataValueField = "Value";
            ddlCountry.DataBind();
        }
        //else
        //{              
        //    ddlState.ID = "ddlState";
        //    ddlState.AutoPostBack = true;
        //    ddlState.SelectedIndexChanged += new EventHandler(ddlState_SelectedIndexChanged);
        //    panel1.Controls.AddAt(2, ddlState);


        //    //ddlDistrict.ID = "ddlDistrict";
        //    //panel1.Controls.AddAt(3, ddlDistrict);
        //}
    }

    protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
    {            
        int value = Convert.ToInt32(ddlCountry.SelectedValue);           
        panel1.Controls.AddAt(2, ddlState);
        //DropDownList ddlState = new DropDownList();                               
        //ddlState.AutoPostBack = true;
        if (value != 0)
        {
            ddlState.DataSource = objBALayer.GetState(value);
            ddlState.DataTextField = "Text";
            ddlState.DataValueField = "Value";
            ddlState.DataBind();
        }                      
    }   
    protected void ddlState_SelectedIndexChanged(object sender, EventArgs e)
    {
        int value = Convert.ToInt32(ddlState.SelectedValue);           
        //DropDownList ddlDistrict = new DropDownList()
        panel1.Controls.AddAt(3, ddlDistrict);
        if (value != 0)
        {
            ddlDistrict.DataSource = objBALayer.GetDistrict(value);
            ddlDistrict.DataTextField = "Text";
            ddlDistrict.DataValueField = "Value";
            ddlDistrict.DataBind();
        }
    }
}

}

0 个答案:

没有答案