Display selectedvalues of listbox as label - multiple values

▼魔方 西西 提交于 2019-12-20 07:46:43

问题


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.


回答1:


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!

Again, sorry for not having VS handy!



来源:https://stackoverflow.com/questions/44052762/display-selectedvalues-of-listbox-as-label-multiple-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!