以逗号获取所有选定的下拉列表值

时间:2016-04-12 07:25:57

标签: c# asp.net

我有一个多选dropdownlist,其中,

如果我选择了3-4 items,则应该一个接一个地以逗号进行查看。

我试过这个

string message = "";
string countItemSelected = "";
foreach (ListItem item in cmbEmp_Name.Items)
{
    if (item.Selected)
    {
        message += item.Value + "\\n";
        countItemSelected = item.Value;
        //Muster_Process();
    }
}

ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);

但它不会出现在逗号中,如何获得?

3 个答案:

答案 0 :(得分:4)

为什么不使用String.Join

var selItems = cmbEmp_Name.Items
    .Cast<ListItem>()
    .Where(x => x.Selected)
    .Select(x => x.Value).ToArray();
var result = String.Join(",", selItems);
var countItemSelected = selItems.Length;

ToArray的调用可以在计算所选项目时避免列表的第二次迭代。

答案 1 :(得分:1)

string message = "";
string countItemSelected = "";
int count = 0;
foreach (ListItem item in cmbEmp_Name.Items)
{
    if (item.Selected)
    {
        if (count > 0)
            message += ",";
        count++;
        message += item.Value;
        //countItemSelected = item.Value;
        //Muster_Process();
    }
}
countItemSelected = count.toString();

ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "alert('" + message + "');", true);

答案 2 :(得分:0)

使用

.button{
    -fx-border-color: transparent;
    -fx-border-width: 0;
    -fx-background-radius: 0;
    -fx-background-color: transparent;
    -fx-font-family:"Segoe UI", Helvetica, Arial, sans-serif;
    -fx-font-size: 1em; /* 12 */
    -fx-text-fill: #828282;
}

.button:focused {
    -fx-border-color: transparent, black;
    -fx-border-width: 1, 1;
    -fx-border-style: solid, segments(1, 2);
    -fx-border-radius: 0, 0;
    -fx-border-insets: 1 1 1 1, 0;
}

.button:pressed {
    -fx-background-color: black;
    -fx-text-fill: white;
}