【问题标题】:Combining Multiple Drop Down List Selections Into 1 Variable将多个下拉列表选择组合到 1 个变量中
【发布时间】:2016-01-17 04:58:10
【问题描述】:

我有一个包含 10 个下拉列表的订单表单,用户可以在其中选择一个项目。我想将所有选定的项目组合成一个用逗号分隔的变量。我这样做的方式是为每个单独的下拉列表使用 if 语句并添加到字符串中,这是最有效的方法吗?这是我的语法的一个子集(不是所有 10 个下拉列表),但你会明白的:

string fullselection = null;

if (dropdownlist1.SelectedIndex > -1) { fullselection += dropdownlist1.SelectedItem.Text; }
if (dropdownlist2.SelectedIndex > -1) { fullselection += "," + dropdownlist2.SelectedItem.Text; }
if (dropdownlist3.SelectedIndex > -1) { fullselection += "," + dropdownlist3.SelectedItem.Text; }
if (dropdownlist4.SelectedIndex > -1) { fullselection += "," + dropdownlist4.SelectedItem.Text; }
if (dropdownlist5.SelectedIndex > -1) { fullselection += "," + dropdownlist5.SelectedItem.Text; }
if (dropdownlist6.SelectedIndex > -1) { fullselection += "," + dropdownlist6.SelectedItem.Text; }

【问题讨论】:

  • 您可以从页面/表单/面板中获取下拉列表并使用 linq 来执行此操作。
  • DropDownList 是数据绑定的吗?如果是,它们是绑定到单个变量还是一个集合?
  • @LucMorin 下拉列表绑定到使用 C# 语法(无数据库)创建的列表

标签: c# asp.net append


【解决方案1】:

这样的事情应该可以工作..

var ddlist = new List<DropdownList>();
ddlist.Add(dropdownlist1);//repeat this for all drowdowns.
var selectedTexts = ddlist.Where(x=>x.SelectedIndex > -1).Select(y=>y.SelectedItem.Text);
var fullselection = String.Join(",",selectedTexts.ToArray());

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-01
    • 2021-12-14
    • 2018-12-16
    • 2020-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多