1

我有一个简单的任务。第一个下拉列表控件是加载国家名称的地方。选择国家名称后,会动态加载对应的州名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();
        }
    }
}

}

4

0 回答 0