将列表框的选定值显示为标签 - 多个值 [英] Display selectedvalues of listbox as label - multiple values

查看:17
本文介绍了将列表框的选定值显示为标签 - 多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为 lstPTLNameDHOD 的列表框,它有多个使用 Ctrl 选择的 PTL 名称.我想以标签或某种方式显示选定的名称,以便提交表单的人可以看到他们究竟是为谁提交的.

I have got a list box called lstPTLNameDHOD which has multiple PTL names which gets selected using Ctrl. I want to display the selected names in a label or some way that the person submitted the form can see who exactly they are submitting it for.

我的问题是我只能在标签上显示一个名称.

My problem is I can only get one name to display on the label.

// Items collection
foreach (ListItem item in lstPTLNameDHOD.Items)
{
   if (item.Selected)
   {
       lbl1stPTL.Text = item.Value.ToString();

   }
}

在更改下拉列表的原因后回发时调用它.

This is being called on post back on the reason dropdown being changed.

推荐答案

您只会显示一个名称,因为您当前的代码将始终显示最后选择的项目的名称.

You are only getting one name to display because your current code would always display name of the last item that is selected.

我手边没有视觉工作室,但你可以试试这个:

I don't have a visual studio handy but you could try this:

StringBuilder sbText = new StringBuilder();
// Items collection
foreach (ListItem item in lstPTLNameDHOD.Items)
{
   if (item.Selected)
   {
       lbl1stPTL.Text = sbText.Append(item.Value.ToString()).ToString();
   }
}

您可能可以通过在两个项目名称之间添加一个空格或逗号来改进这一点,但我希望您明白这个想法并且它会有所帮助!

You can probably refine this by adding a space or a comma between two item names but I hope you get the idea and it helps!

再次,很抱歉没有 VS!

Again, sorry for not having VS handy!

这篇关于将列表框的选定值显示为标签 - 多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆