gpt4 book ai didi

c# - ListBox 属性 (C#) 中的所选项目出错?

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:37 25 4
gpt4 key购买 nike

for (int counter = 0; counter < countSelected; counter++)
{
string groupName = txt_GroupName.Text;
//Get GroupID from the created group
string GroupIDQueryText = "SELECT GroupID FROM tbl_group WHERE GroupName ";
int groupID = Convert.ToInt32(server.performQuery(GroupIDQueryText, groupName, MySqlDbType.VarChar));
//To get User ID
string firstName = ListBoxMembers.SelectedItems[counter].Value;
}

这不是返回选定的值,而是返回列表中的第一个人,即使我没有选择它。我哪里错了?System.Web.UI.WebControls 不包含 listboxmembers.selectedItems 错误的定义

最佳答案

您正在使用 .Items,它是 ListBox 中所有项目的集合。我认为您打算使用 .SelectedItems ( documentation on MSDN )。

// When counter = 0, this is the very first item in the listbox
ListBoxMembers.Items[counter].Value;

// When counter = 0, this is the first of the selected items in the listbox
ListBoxMembers.SelectedItems[counter].Value;

编辑 Web ListBox 控件与 WinForms ListBox 控件不同,因此了解上下文非常有值(value)。这是一篇来自 MSDN 的文章 how to determine the selected items in a multi-selection list control (向下滚动到多选部分)。这个想法是遍历所有 .Items 并检查每个的 .Selected 属性。

关于c# - ListBox 属性 (C#) 中的所选项目出错?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7405959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com