C# 具有SelectedIndex=0的DropDownList

C# 具有SelectedIndex=0的DropDownList,c#,asp.net,sql,drop-down-menu,C#,Asp.net,Sql,Drop Down Menu,我编写了一个简单的程序,它与数据库连接,从两个表中读取数据,并将它们写入两个DropDownLists(DDL)。DDL具有类似父级和子级的结构。这是我的代码: protected void Page_Load(object sender, EventArgs e) { FillControls(); if (!IsPostBack) { ddl_SelectedIndexChanged((DropDownList)PlaceHolder1.FindCon

我编写了一个简单的程序,它与数据库连接,从两个表中读取数据,并将它们写入两个DropDownLists(DDL)。DDL具有类似父级和子级的结构。这是我的代码:

protected void Page_Load(object sender, EventArgs e)
{
    FillControls();
    if (!IsPostBack)
    {
        ddl_SelectedIndexChanged((DropDownList)PlaceHolder1.FindControl("ddlTurCountry"), null);
    }
    else
    {
        if (((DropDownList)PlaceHolder1.FindControl("ddlTurCountry")).SelectedIndex == 0) ddl_SelectedIndexChanged((DropDownList)PlaceHolder1.FindControl("ddlTurCountry"), null); //without this string dropDownList doesn't call ddl_SelectedIndexChanged() when it's SelectedIndex=0
    }
}

private void FillControls()
{
    Panel panel = new Panel();
    Label lbl = new Label();
    lbl.Text = "Country: ";
    panel.Controls.Add(lbl);
    DropDownList ddl = new DropDownList();
    ddl.ID = "ddlTurCountry";
    ddl.DataTextField = "CountryName";
    ddl.DataValueField = "CountryName";
    ddl.SelectedIndexChanged += new EventHandler(ddl_SelectedIndexChanged);
    ddl.AutoPostBack = true;
    panel.Controls.Add(ddl);
    panel.Controls.Add(new LiteralControl("<br />"));

    lbl = new Label();
    lbl.Text = "Kurort: ";
    panel.Controls.Add(lbl);
    ddl = new DropDownList();
    ddl.ID = "ddlTurKurort";
    ddl.DataTextField = "KurortName";
    ddl.DataValueField = "KurortName";            
    panel.Controls.Add(ddl);
    PlaceHolder1.Controls.Add(panel);

    FillDDL("ddlTurCountry", "SELECT CountryName from Countries");
}

private void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    if (((DropDownList)sender).ID == "ddlTurCountry")
    {
        FillDDL("ddlTurKurort", "SELECT Kurorts.KurortName FROM Kurorts INNER JOIN Countries ON Countries.idCountry = Kurorts.idCountry WHERE (Countries.CountryName = N'" + ((DropDownList)PlaceHolder1.FindControl("ddlTurCountry")).SelectedValue + "')");
    }
}

private void FillDDL(string ddlID, string SQLCommand)
{
    ((DropDownList)PlaceHolder1.FindControl(ddlID)).DataSource = GetDataFromDB(SQLCommand);
    ((DropDownList)PlaceHolder1.FindControl(ddlID)).DataBind();
}

private DataTable GetDataFromDB(string SQLCommand)
{
    DataTable dt = new DataTable();
    string sqlcnString = @"Data Source=.\SQLEXPRESS;Initial Catalog=turs;Integrated Security=True;Pooling=False";
    SqlConnection sqlcn = new SqlConnection(sqlcnString);
    if (sqlcn.State.ToString() == "Closed") sqlcn.Open();
    SqlDataAdapter dataAdapter = new SqlDataAdapter(SQLCommand, sqlcn);
    dataAdapter.Fill(dt);
    sqlcn.Close();
    return dt;
}
加载到
页面,但我不喜欢这样,我不明白为什么当
选择dex=0
时它不起作用。


已解决


每次页面刷新时,您都会填充下拉列表。不要那样做。 仅在以下情况下调用FillControls()!第页,回发

protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        FillControls();   
        //Not sure why you have this here, probably not needed
        ddl_SelectedIndexChanged((DropDownList)PlaceHolder1.FindControl("ddlTurCountry"), null);
    }
    else
    {

    }
}

回发后,所有内容都会消失,因为oll控件会创建动态dropdownlist为什么需要动态创建?您不能将其放在您的aspx页面上吗?您还可以查看以下内容:
    protected void Page_Init(object sender, EventArgs e)
    {
        FillControls();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack) ddl_SelectedIndexChanged((DropDownList)PlaceHolder1.FindControl("ddlTurCountry"), null);
    }
protected void Page_Load(object sender, EventArgs e)
{

    if (!IsPostBack)
    {
        FillControls();   
        //Not sure why you have this here, probably not needed
        ddl_SelectedIndexChanged((DropDownList)PlaceHolder1.FindControl("ddlTurCountry"), null);
    }
    else
    {

    }
}