C# 从列表框中检索每个单独的项并将其存储为字符串
在aspx.cs中C# 从列表框中检索每个单独的项并将其存储为字符串,c#,asp.net,listbox,C#,Asp.net,Listbox,在aspx.cs中 string CatID = string.Empty; foreach (ListItem li in ListBox1.Items) { if (li.Selected == true) { // get the value of the item in your loop CatID += li.Value + ","; } } 在aspx中 &
string CatID = string.Empty;
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
{
// get the value of the item in your loop
CatID += li.Value + ",";
}
}
在aspx中
<asp:ListBox ID="ListBox1" runat="server" DataSourceID="SqlDataSource1" SelectionMode="Multiple"
DataTextField="Username" DataValueField="Username" Height="175px"
Width="167px"></asp:ListBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>"
SelectCommand="SELECT * FROM [login_det] WHERE ([Type] = @Type)">
<SelectParameters>
<asp:Parameter DefaultValue="User" Name="Type" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
另一个冲突是列表框中可能有100个项目。如果我选择了90个项目,我应该用一个字符串变量声明或分配每个项目,这是没有意义的。但是是否还有其他可能存储或检索列表框中的单个项目
string CatID = string.Empty;
int i = ListBox1.Items.Count(); //not sure with this
string[] CatValues = new string[i];
int iCount = 0;
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected == true)
{
// get the value of the item in your loop
CatID += li.Value + ",";
CatValues[iCount] = li.Value;
iCount++;
}
}
然后可以循环CatValues以获取所有值。我不知道这是否有助于您使用列表:
List<string> result = new List<string>();
foreach (ListItem li in ListBox1.Items)
{
if (li.Selected)
{
// get the value of the item in your loop
result.Add(li.Value);
}
}
列表结果=新列表();
foreach(ListBox1.Items中的ListItem li)
{
如果(li.选定)
{
//获取循环中项目的值
结果.加(li.值);
}
}
您可以使用Linq提取所有选定值并将其添加到列表中,如下所示:
var selectedValues = from ListItem item in ListBox1.Items where item.Selected select item.Value;
var selectedStrings = selectedValues.ToList();
您可以使用集合,例如列表
来存储值;Linq可以很方便地为收藏提供素材
List<String> result = ListBox1.Items.Cast<ListItem>().Where(x => x.Selected).Select(x => x.Value).ToList();
List result=ListBox1.Items.Cast().Where(x=>x.Selected).Select(x=>x.Value).ToList();
使用Linq还可以很容易地创建逗号分隔的字符串,如下所示:
string CatID = String.Join(",", ListBox1.Items.Cast<ListItem>().Where(x => x.Selected).Select(x => x.Value));
string CatID=string.Join(“,”,ListBox1.Items.Cast()。其中(x=>x.Selected)。选择(x=>x.Value));
根据您的评论,您需要所选项目的列表来进行进一步处理。您没有显示实际代码,因此我将使用一个虚构的示例:
// Let me suppose you're using EF to store some data
using (var context = new DatabaseContext())
{
foreach (var li in ListBox1.Items.Cast<ListItem>().Where(x => x.Selected))
{
// I assume you have user ID (as it's name) in the Value property
// of each ListItem in your ListBox
string userName = li.Value;
var user = context.Users.FirstOrDefault(x => x.Name == userName);
// This is not redundant, user may even be deleted in another session
if (user != null)
{
// Do changes you need
user.HasBeenSelected = true;
}
}
// We're done, apply changes you made
context.SaveChanges();
}
//假设您正在使用EF存储一些数据
使用(var context=new DatabaseContext())
{
foreach(ListBox1.Items.Cast()中的var li,其中(x=>x.Selected))
{
//我假设您在Value属性中有用户ID(作为它的名称)
//列表框中每个列表项的
字符串userName=li.Value;
var user=context.Users.FirstOrDefault(x=>x.Name==userName);
//这不是多余的,用户甚至可能在另一个会话中被删除
如果(用户!=null)
{
//你需要改变吗
user.hasbeenselect=true;
}
}
//我们完成了,应用您所做的更改
SaveChanges();
}
正如您所看到的,您不需要为每个选定项创建单独的变量,您有一个它们的列表,然后您不需要创建另一个临时列表,只需使用选定项进行您必须进行的处理
抱歉,如果没有要检查的代码,我无法更具体地说明,这只是另一个明确说明意图的示例:
foreach (var li in ListBox1.Items.Cast<ListItem>().Where(x => x.Selected))
{
var user = Membership.GetUser(li.Value);
if (user != null)
{
user.IsLockedOut = false;
Membership.UpdateUser(user);
}
}
foreach(ListBox1.Items.Cast()中的变量li,其中(x=>x.Selected))
{
var user=Membership.GetUser(li.Value);
如果(用户!=null)
{
user.IsLockedOut=false;
成员资格。更新用户(用户);
}
}
根据您的示例(仅用于说明!):
使用(var连接=新的SqlConnection(ProjectConnectionString))
{
connection.Open();
foreach(ListBox1.Items.Cast()中的var li,其中(x=>x.Selected))
{
使用(var command=connection.CreateCommand())
{
command.CommantText=
“UPDATE[Login_det]SET Enabled=0,其中Name=@UserName”;
command.Parameters.AddWithValue(“用户名”,li.Value);
command.ExecuteNonQuery();
}
}
}
请注意,如果您有一个不需要执行多个命令的项目列表,则可以使用带有表参数的单个命令。请参见下面的示例。列表框中的数字和选定项是运行时的事情。变量是编译时的东西。你走错路了。我建议您向我们展示您想要实现的目标…我想将单个选定项存储为字符串变量。是的,您说了,但您不能(编译时、运行时)。解释为什么您不想按您希望的方式这样做。在我的列表框中,我有用户列表
。因此,选择用户应该使我向个人用户
配置文件插入一些数据。因此,我需要个人记录。“li.Selected==true”是多余的;“如果(li.Selected)”很重要better@ShreyasTg添加了另一个示例,我仍然不知道您必须做什么(不仅仅是数据来自哪里),但它可能与您正在做的类似(只是调整SQL代码…这只是一个示例,我不会用那种方式硬编码SQL代码)
foreach (var li in ListBox1.Items.Cast<ListItem>().Where(x => x.Selected))
{
var user = Membership.GetUser(li.Value);
if (user != null)
{
user.IsLockedOut = false;
Membership.UpdateUser(user);
}
}
using (var connection = new SqlConnection(ProjectConnectionString))
{
connection.Open();
foreach (var li in ListBox1.Items.Cast<ListItem>().Where(x => x.Selected))
{
using (var command = connection.CreateCommand())
{
command.CommantText =
"UPDATE [Login_det] SET Enabled = 0 WHERE Name = @UserName";
command.Parameters.AddWithValue("UserName", li.Value);
command.ExecuteNonQuery();
}
}
}