具有SelectedIndex = 0的DropDownList

时间:2014-02-17 20:09:03

标签: 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.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;
}

此代码无效。当我运行这个程序时,它启动良好,两个DDL填充数据。然后我开始在第一个DDL中更改Value并且它也可以工作,但只有在我不选择第一个值时它才有效。

如果我选择第一个值,则第二个DDL不会填充。当我使用调试器观察时,我发现当第一个DDL的SelectedIndex等于零时,方法ddl_SelectedIndexChanged不适用。在这种情况下,我添加了这一行:

if (((DropDownList)PlaceHolder1.FindControl("ddlTurCountry")).SelectedIndex == 0) 
    ddl_SelectedIndexChanged((DropDownList)PlaceHolder1.FindControl("ddlTurCountry"), null); 

进入Page_Load,但我不喜欢这个,我不明白为什么它在selectedIndex=0时不起作用。


解决

    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);
    }

1 个答案:

答案 0 :(得分:1)

每次页面刷新时,您都会填充下拉列表。不要那样做。 如果!Page.Postback

,只调用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
    {

    }
}