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

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

问题描述

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

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!

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

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