示例#1
0
    protected void InitCarsList(object sender, EventArgs e)
    {
        try
        {
            CheckBoxList    checkBoxList = sender as CheckBoxList;
            List <ListItem> carsLists    = new List <ListItem>();
            if (checkBoxList != null && Request.QueryString["JobID"] != null)
            {
                var jobslist     = SiteUtilities.GetJobsByJobId(new Guid(Request.QueryString["JobID"]));
                var allcars      = SiteUtilities.GetAllCars();
                var existingcars = SiteUtilities.GetCarFromJobID(jobslist[0].JobId);


                foreach (Car existingcar in existingcars)
                {
                    for (int i = 0; i < allcars.Count; i++)
                    {
                        if (existingcar.CarId == allcars[i].CarId)
                        {
                            string   name = allcars[i].Name;
                            ListItem item = new ListItem(name, allcars[i].CarId.ToString());
                            item.Selected = true;
                            carsLists.Add(item);
                            allcars.Remove(allcars[i]);
                        }
                    }
                }

                for (int i = 0; i < allcars.Count; i++)
                {
                    string   name = allcars[i].Name;
                    ListItem item = new ListItem(name, allcars[i].CarId.ToString());
                    carsLists.Add(item);
                }
            }
            else
            {
                var allcars = SiteUtilities.GetAllCars();
                for (int i = 0; i < allcars.Count; i++)
                {
                    string   name = allcars[i].Name;
                    ListItem item = new ListItem(name, allcars[i].CarId.ToString());
                    carsLists.Add(item);
                }
            }



            checkBoxList.DataSource = carsLists;
            checkBoxList.DataBind();

            for (int i = 0; i < checkBoxList.Items.Count; i++)
            {
                if (checkBoxList.Items[i].Text == carsLists[i].Text)
                {
                    if (carsLists[i].Selected)
                    {
                        checkBoxList.Items[i].Selected = true;
                        checkBoxList.Items[i].Value    = carsLists[i].Value;
                    }
                    else
                    {
                        checkBoxList.Items[i].Value = carsLists[i].Value;
                    }
                }
            }
        }
        catch
        {
        }
    }