[英]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.