2017-05-08 3 views
0

ドロップダウンがあり、選択したアイテムのみを表示したいのですが、同じアイテムに対して2回表示されます。たとえば、アイテム#4と#7を選択すると、このように表示されます。4 4、7 7.ドロップダウンから選択したアイテムのユニークな値のみを表示するにはどうすればよいですか?C#を使用してDropDownCheckBoxesから選択したアイテムのみを表示する

string myList = string.Empty; 
foreach (System.Web.UI.WebControls.ListItem item in facDDL.Items) 
{ 
    if (item.Selected) 
    { 
     myList += item.Text + " " + item.Value + ","; 
    } 
} 

答えて

0

これは、テキストと値の両方を使用しているためです。 、あなたの時に選択リスト項目で4行目をこの場合

<asp:DropDownList ID="UserSelectionDropDownControl" runat="server"> 
       <asp:ListItem Value="1">1</asp:ListItem> 
       <asp:ListItem Value="2">2</asp:ListItem> 
       <asp:ListItem Value="3">3</asp:ListItem> 
       <asp:ListItem Value="4">Four</asp:ListItem> 
       <asp:ListItem Value="5">Five</asp:ListItem>     
</asp:DropDownList> 

は、あなたがより良いまでごドロップの内容を変更することにより、効果を理解する https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listitem.value(v=vs.110).aspx

を見てください文字列は次のようになります

四4、

0

私の推測では、KeyItemのペアをListItemプロパティとして指定しないでください。

あなたは

<asp:ListItem>Text</asp:ListItem> 

aspxページ上でこれを行う場合は、キーと値の両方が同じです。あなたが背後にあるコード内のデータを結合しているので、もしあなたが

<asp:ListItem Text="Value" Value="3"></asp:ListItem> 

にそれを変更する必要があり、あなたがキーと値columsを指定してください。

DropDownList1.DataSource = mySource; 
DropDownList1.DataTextField = "Text"; 
DropDownList1.DataValueField = "Value"; 
DropDownList1.DataBind(); 

それとも

DropDownList1.Items.Insert(0, new ListItem("Text", "Value", true)); 
0

背後にあるコードから一度に1を追加するときに私がやったことはすべて、この

myList += "'" + item.Value + "', "; 

、それにこの

myList += item.Text + " " + item.Value + ","; 

を交換することです魅力的に働いた。 ありがとう

関連する問題