Checkboxlist按逗号分隔字符串填充 [英] Checkboxlist population by comma separated string

查看:88
本文介绍了Checkboxlist按逗号分隔字符串填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string []k = objP.Salary.Split(',');
            for (int m = 0; m <= k.Length - 1; m++)
            {
                for (int i = 0; i <= chklstSalary.Items.Count; i++)
                {
                    if (chklstSalary.Items[i].Value == k[m])
                    {
                        chklstSalary.Items[m].Selected = true;
                    }
                }
            }









i想重新填充checkboxlist这是一个逗号分隔的字符串我的代码是这个



但是我收到一条错误消息







i want to repopulate the checkboxlist which is a comma separate string my code is this

but i am getting an error message

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index



plz帮我解决问题...


plz help me to solve my problem...

推荐答案

尝试: chklstSalary.Items.Count -1

,你使用i代替chklstSalary并且不能与m混合(当您设置所选时)。试试这个:

Try: chklstSalary.Items.Count -1
also, you use i for chklstSalary and cannot mix it with m (when you set the selected). Try this:
if (chklstSalary.Items[i].Value == k[m])
{
   chklstSalary.Items[i].Selected = true;
   break; // no need to loop any further because we already found the matching checkbox
}



祝你好运!


Good luck!


或者你只需​​指定checkboxlist的数据源proprty作为包含值的数组
alternatively u can just specify the datasource proprty of checkboxlist as the array containing values


看起来太复杂了。

我先尝试这样的事情:



Looks too complicated for what it's supposed to do.
I would try something like this first:

char[] delimiter = { ',' };
string[] k = objP.Salary.Split(delimiter, StringSplitOptions.RemoveEmptyEntries);
            foreach (string m in k)
            {
                foreach(item i in chklstSalary)
                {
                   if (i.Value == m)
                    {
                        chklstSalary.i.Selected = true;
                    }
                }
            }


这篇关于Checkboxlist按逗号分隔字符串填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆