ASP.NET将ListBox控件转换为DropDownList

时间:2012-01-26 03:36:38

标签: c# asp.net visual-studio

我正在使用VS2005 C#。

我正在尝试将我的ListBox控件转换为使用DDL。

然而,在我将一个用户列表添加到特定角色的功能中,它可以在ListBox中启用多个选择。

我想将其更改为使用DDL,只允许单一选择。

以下是代码:

    string[] newusers = new string[UsersListBox.GetSelectedIndices().Length];

    for (int i = 0; i < newusers.Length; i++)
    {
        newusers[i] = UsersListBox.Items[UsersListBox.GetSelectedIndices()[i]].Value;
    }

错误指出System.Web.UI.WebControls.DropDownList' does not contain a definition for 'GetSelectedIndices

我是否知道如何使用DDL而不是ListBox来改变我的代码?

1 个答案:

答案 0 :(得分:2)

DropDownList无法选择多个项目,因此您没有注意到GetSelectedIndices()方法。而是有一个SelectedIndex属性。

string newuser = null;
if (UserListBox.SelectedIndex > -1)
{
    newuser = UsersListBox.Items[UsersListBox.SelectedIndex].Value;
}
相关问题