I have an html table that I build dynamically. The table holds a couple of submit buttons and a list of items from a database with a checkbox before each of them. So the user is allowed to check as many items to be copied over to another table. I was trying to add another table row above what I just described simply to create headers for my html table. I have three columns and I would like the users to understand what they are looking at. But when I add the row I get a 'Specified argument was out of the range of valid values.
Parameter name: index' and it highlights this line of code: if (ctrl.Controls[0].Controls[0] is CheckBox)
So it looks like once I add that table row, it messes up my control numbering?
below is a snippet of my code:
Table tblMain = new Table();
tblMain.BorderStyle =
BorderStyle.Solid;
tblMain.Width = 750;
TableRow tr;
TableCell td1;
TableCell td2;
TableCell td3;
SPSite spSiteContractor = new SPSite(strSecuritySiteUrl);
SPWeb spWebContractor = spSiteContractor.OpenWeb();
///Contractors
SPList spListContractor = spWebContractor.Lists["Daily Contractor"];
SPListItemCollection listItems = spListContractor.Items;
string txtVendor;
string txtName;
//Set table row headers
//THIS IS THE CODE THAT TRIGGERS THE ERROR
tr =
new TableRow();
td1 = new TableCell();
td2 = new TableCell();
td3 = new TableCell();
tr.Cells.Add(td1);
tr.Cells.Add(td2);
tr.Cells.Add(td3);
td1.Font.Size = 8;
td1.Font.Bold = true;
td1.Text = "Contractor Name";
td2.Font.Size = 8;
td2.Font.Bold = true
td2.Text = "Vendor";
tblMain.Rows.Add(tr);
for (int i = 0; i < listItems.Count;i++ )
{
SPListItem item = listItems[i];
txtVendor =
string.Empty;
txtName =
string.Empty;
if (item["Vendor"] != null)
{
txtVendor = item[
"Vendor"].ToString();
}
if (item["Last_x0020_Name"] != null)
{
txtName = item[
"Last_x0020_Name"].ToString();
}
CheckBox chk = new CheckBox();
contractorInfo =
new KeyValuePair<string, string>(txtVendor,txtName);
alllist.Add(chk, contractorInfo);
chk.Text = txtName;
tr =
new TableRow();
td1 =
new TableCell();
td2 =
new TableCell();
td3 =
new TableCell();
tr.Cells.Add(td1);
tr.Cells.Add(td2);
tr.Cells.Add(td3);
td1.Controls.Add(chk);
td1.Font.Size = 8;
td2.Text = txtVendor;
td2.VerticalAlign =
VerticalAlign.Bottom;
td2.Font.Size = 8;
tblMain.Rows.Add(tr);
}
PlaceHolder1.Controls.Add(tblMain);
tblMain.Dispose();
btnCheckInContractor =
new Button();
btnCheckInContractor.CausesValidation =
true;
btnCheckInContractor.CssClass =
"ms-ButtonHeightWidth";
btnCheckInContractor.Text =
"Check-In";
btnCheckInContractor.Click +=
new EventHandler(btnCheckInContractor_Click);
PlaceHolder1.Controls.Add(btnCheckInContractor);