2014-02-17 49 views
0

我寫了一個簡單的程序,它連接數據庫,並從兩個表讀取數據,並將它們寫入兩個DropDownLists(DDL)。 DDL具有像父母和孩子一樣的結構。這是我的代碼:DropDownList SelectedIndex = 0

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,它也可以工作,但只有在不選擇第一個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

你填充下拉每次您的列表頁面刷新。不要這樣做。 只有調用FillControls()如果!Page.Postback

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 
    { 

    } 
} 
+0

一切回發後即消失,因爲OLL控件創建動態 – mit

+0

爲什麼在下拉列表需要動態創建?你不能把它放在你的aspx頁面上嗎? –

+1

你也可以看看這個:http://www.aspsnippets.com/Articles/Creating-Dynamic-DropDownList-Controls-in-ASP.Net.aspx –

相關問題