简体   繁体   English

如何从asp.net中的数组中的多选下拉列表中存储选定值的列表?

[英]How to Store List of selected value from multiselect dropdown list in array in asp.net?

I used Bootstrap multiselect dropdown item, in which if I select many values it only select the first values, whats wrong in my code logic. 我使用了Bootstrap multiselect下拉项,其中如果我选择许多值,它只会选择第一个值,这在我的代码逻辑中出了什么问题。

Asp dropdown list ASP下拉列表

<asp:dropdownlist  Width="320px" id="drplstGetDb" runat="server"  multiple="Multiple">  </asp:dropdownlist>

Javascript to enable multiple check feature from bootstrap. Javascript,可从引导程序启用多重检查功能。

$(function() {
    $('[id*=drplstGetDb]').multiselect({
        includeSelectAllOption: true
    });
});

C# Code to store in array. C#代码存储在数组中。

int[] stopWordArray = new int[drplstGetDb.Items.Count];

foreach(ListItem listItem in drplstGetDb.Items) {
    if (listItem.Selected) {
        int i = 0;
        stopWordArray[i] = Convert.ToInt32(drplstGetDb.Items[i].Value.ToString());
        i++;
    }
}

I am only getting first checked value in array, If I don't use this if if (listItem.Selected) I can Store all values in array. 我只得到数组中的第一个检查值,如果if(listItem.Selected)我不可以使用它,则可以将所有值存储在数组中。 Can anyone suggest about this error,, Specially with Selected Logic. 任何人都可以提出有关此错误的建议,特别是与Selected Logic有关。 or Alternative to this.. 或替代。

Because you are setting 因为你在设定

int i = 0 

inside the loop, you are setting the first array element of stopWordArray ALL THE TIME, no matter what. 在循环内部,无论何时,您始终都在设置stopWordArray的第一个数组元素。 Move the 移动

int i = 0 

to the outside of the foreach and you should have better luck there. 到foreach的外部,您应该在那里拥有更好的运气。

Try this 尝试这个

int[] stopWordArray = new int[drplstGetDb.Items.Count];
int i = 0;
foreach(ListItem listItem in drplstGetDb.Items)
{
    if (listItem.Selected)
    {
        stopWordArray[i] = Convert.ToInt32(listItem.Value.ToString()); //something like this
        i++;
    }
}

Maybe even easier would be to get rid of the int array and use a list 甚至更容易的是摆脱int数组并使用列表

List<int> stopWordArray = new List<int>();

foreach (ListItem listItem in drplstGetDb.Items)
 {
     if (listItem.Selected)
      {
          stopWordArray.Add(Convert.ToInt32(listItem.Value.ToString()));
      }
 }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试根据 ASP.NET Web 应用程序中的下拉列表和日期选择器中的选定值进行年龄验证 - Trying to do Age Validation based on selected value from dropdown list and datepicker in ASP.NET Web application 如何使用jQuery从多选下拉列表中打印选定的值? - how to print selected values from multiselect dropdown list using jquery? 使用Knockout.js如何从数组中的多选下拉列表中提取所选项目 - Using Knockout.js how to extract selected items from a multiselect dropdown list in an array 在 Asp.net core Mvc 中提交表单后,下拉列表未保留所选值 - Dropdown list is not retaining the selected value after submitting the form in Asp.net core Mvc 如何捕获从下拉列表中选择的值? - How to capture value selected from a dropdown list? 如何将值从一个下拉列表存储到另一个下拉列表 - How to store value from one dropdown list to another dropdown list asp.net:设置为下拉列表选择的默认值 - asp.net : set default value selected for a drop down list ASP.NET/JAVASCRIPT从RadioButton值(客户端)禁用下拉列表 - ASP.NET/JAVASCRIPT disabling dropdown list from RadioButton value (client side) 使用JavaScript从Boostrap Dropdown列表中的数据库ASP.NET Web API获取价值 - Get value from database ASP.NET web api in Boostrap Dropdown list using JavaScript 将值从下拉列表传递到ASP.NET MVC中的数据库 - Passing values from dropDown list to Database in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM