2011-10-25 39 views
1

自動回來後的第一項下面是簡單的代碼:我們可以做下拉列表在asp.net列表

protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e) 
{ 

    string word = DropDownList1.SelectedItem.Text; 

    generateSynomyn(word); 

} 

當我選擇在指數中的第一項,不執行該代碼。爲了執行第一個項目的代碼,我必須先選擇任何其他項目,然後只有dropdownlist中的第一個項目。

此外,我們是否可以自動回覆或至少使selectItemdropdownlist的第一項?

+0

對不起,可能是誤解了這一點。爲什麼你不能在你的Page_Load事件中添加這兩行? – twsaef

回答

1

爲了使DropDownList火,你應該進入一個空白項目的第一個項目,所以選擇要作出:

//put this where you bind your items the first time 
DropDownList1.Items.Insert(0, ""); //adds a blank item first in the list 
//or something like this 
DropDownList1.Items.Insert(0, new ListItem("[ select an item ]","")); 
0

添加這對您的下拉你的預渲染事件:

DropDownList1.Insert(0, new ListItem("-Select One-", "0")); 
DropDownList1.SelectedIndex = 0; 

Regards