我们可以在asp.net中自动回发列表中的第一项

时间:2011-10-25 03:25:29

标签: c# asp.net

这是简单的代码:

protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{

    string word = DropDownList1.SelectedItem.Text;

    generateSynomyn(word);

}

当我选择索引上的第一个项目时,不会执行此代码。为了执行第一个项目的代码,我必须先选择任何其他项目,然后只选择dropdownlist中的第一个项目。

此外,我们是否可以自动回复或至少将selectItem作为dropdownlist上的第一项?

2 个答案:

答案 0 :(得分: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 ]",""));

答案 1 :(得分:0)

在DropDown的预呈现事件中添加此内容:

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

此致