Avatar of creejohnson
creejohnson

asked on 

How do I loop through a Calendar control to get the controls inside of cells?

I have created a Calendar control that filters a data table based upon the date of the rows in the data table being equal to the individual cell date. If the rows have data, it produces a checkboxlist inside the cell with the various lunch menu choices. When the user clicks a button (Add to Cart), I want to loop through the checkboxlists and get the values of the checkboxes (productIds) if they are checked. The checkboxlist has an ID based upon the service date in question, so I want to access that as well. Any ideas would be greatly appreciated. I did look at this suggestion but wasn't sure of how it worked...
https://www.experts-exchange.com/questions/22649398/Access-controls-within-a-day-of-asp-calendar.html

I'll post some code below:


DateTime daDate = DateTime.Parse(e.Day.Date.ToString());
 
            if (!e.Day.IsOtherMonth && !e.Day.IsWeekend)
            {
                // Set the width as it will have data
                e.Cell.Width = 140;
 
                DataRow[] rowsExisting = dtExisting.Select("LunchDate = '" + daDate + "'");
                DataRow[] rowsService = dtService.Select("ServiceDate = '" + daDate + "'", "CategoryName ASC, ProductName DESC");
 
                if (rowsExisting.Length == 0 && rowsService.Length == 0)
                {
                    Label labelNone = new Label();
                    labelNone.ForeColor = System.Drawing.Color.Black;
                    labelNone.Font.Name = "Verdana";
                    labelNone.Font.Size = FontUnit.Point(11);
                    labelNone.Font.Bold = true;
                    labelNone.Text = "No Lunches Available";
                    e.Cell.Controls.Add(labelNone);
                }
 
                // Create a label to print ordered info of each returned row.
                for (int i = 0; i < rowsExisting.Length; i++)
                {
                    Label label = new Label();
                    label.Text = rowsExisting[i][0].ToString() + " ea. " + rowsExisting[i][3].ToString();
                    e.Cell.Controls.Add(label);
                }
 
                // Create a check box control to use for ordering of each returned row.
                if (rowsService.Length > 0)
                {
                    CheckBoxList checkboxlist = new CheckBoxList();
                    checkboxlist.RepeatColumns = 1;
                    checkboxlist.RepeatDirection = RepeatDirection.Vertical;
                    checkboxlist.ID = rowsService[0][2].ToString();
                    string category = "";
 
                    for (int i = 0; i < rowsService.Length; i++)
                    {
                        // Get the value of the new category
                        string newCategory = rowsService[i][0].ToString();
 
                        if (category == newCategory)
                        {
                            // nada
                        }
                        else
                        {
                            checkboxlist.Items.Add(new ListItem(newCategory, newCategory, false));
                        }
 
                        checkboxlist.Items.Add(new ListItem(rowsService[i][1].ToString() + " @ " + String.Format("{0:c}", rowsService[i][5]), rowsService[i][3].ToString()));
                        category = newCategory;
 
                        //CheckBox checkbox = new CheckBox();
                        //checkbox.ID = rowsService[i][2].ToString() + "_" + rowsService[i][3].ToString();
                        //checkbox.Text = rowsService[i][1].ToString() + " @ " + String.Format("{0:c}", rowsService[i][5]);
                        //e.Cell.Controls.Add(checkbox);
                        //Response.Write("<br />");
                    }
 
                    // add the list to the cell
                    e.Cell.Controls.Add(checkboxlist);
 
                }
 
                e.Cell.BackColor = System.Drawing.Color.LightGoldenrodYellow;

Open in new window

.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
creejohnson

8/22/2022 - Mon