简体   繁体   English

为什么“列表框多项选择”在页面加载时不起作用?

[英]Why Listbox Multiple selection is not working on page load?

I have a listbox with data from SQL DB. 我有一个包含来自SQL DB的数据的列表框。 On Page Load,I want to select multiple items according to the data from my query result. 在页面加载中,我想根据查询结果中的数据选择多个项目。 It doesn't give me any errors and also not working. 它不会给我任何错误,也无法正常工作。 Here is the code. 这是代码。

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack){

             DataTable userinfo = AppDataAccess.retrieveUsers(id);
                   foreach (DataRow row in userinfo.Rows)
            {
              string group = row["GroupNumber"].ToString();
                List<string> val = group.Split(',').ToList();

             if (val != null)
                    {
                        ListBox1.SelectionMode = ListSelectionMode.Multiple;

                        //loop to select multiple items
                        foreach (string per in val)
                        {
                            if (ListBox1.Items.FindByValue(per.ToString()) != null)
                            {
                                ListBox1.Items.FindByValue(per.ToString()).Selected = true;
                            }
                        }
                    }
             }



     }
 }

It doesn't give me any error and also not selecting any items.I try several ways and still not working. 它不会给我任何错误,也不会选择任何项目。我尝试了几种方法,但仍然无法正常工作。 Any idea? 任何想法?

You could try it the other way around, loop all ListItems and set each Selected property: 您可以尝试其他方法,循环所有ListItems并设置每个Selected属性:

foreach(DataRow row in userinfo.Rows)
{
    string group = row.Field<String>("GroupNumber");
    string[] vals = group.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
    foreach(ListItem item in ListBox1.Items)
    {
        item.Selected = vals.Contains(item.Value);
    }
}

Remove this items election code from your PageLoad and put it in another method (say SelectItems ) Somewhere in your page you should have one or more ListBox1.DataBind(); 从PageLoad中删除此项目选择代码,并将其放入另一个方法中(例如SelectItems ),在页面的某个位置上应该有一个或多个ListBox1.DataBind(); calls. 电话。 Put a call to SelectItems right after these ListBox1.DataBind(); 在这些ListBox1.DataBind();之后,立即调用SelectItems ListBox1.DataBind(); calls. 电话。

Also I guess your code could be written like this using Linq 而且我猜你的代码可以使用Linq这样写

public void SelectItems()
{
 ListBox1.SelectionMode = ListSelectionMode.Multiple;
 var userinfos = AppDataAccess.retrieveUsers(id);

 var val = userInfos.Rows.SelectMany(r=>r["GroupNumber"].ToString().Split(','))
    .Distinct().ToList()

  //loop to select multiple items // could also be converted to Linq. Not sure it would be useful
  foreach (string per in val)
      {
       if (ListBox1.Items.FindByValue(per.ToString()) != null)
          {
           ListBox1.Items.FindByValue(per.ToString()).Selected = true;
           }
        }

} }

为ListBox设置SelectionMode="Multiple"

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM