Object reference not set to an instance of an obje
I am creating a set of drop down lists on a page with the following code:
Table table = new Table();
TableRow trow = new TableRow();
TableCell tcell = new TableCell();
DropDownList ddl;
Label lbl;
table.ID = "tblItems";
table.BorderColor = System.Drawing.Color.Blue;
table.BorderStyle = BorderStyle.Solid;
table.BorderWidth = 1;
int i = 0;
foreach (DataRow row in dtColumns.Rows)
{
trow = new TableRow();
tcell = new TableCell();
tcell.ID = "Cell" + i.ToString();
tcell.Width = 120;
ddl = new DropDownList();
lbl = new Label();
lbl.Text = row["Column_Name"].ToString();
tcell.Controls.Add(lbl);
trow.Cells.Add(tcell);
tcell = new TableCell();
ddl.DataSource = dt;
ddl.DataTextField = "Choice";
ddl.DataValueField = "Reference";
ddl.DataBind();
ddl.SelectedValue = row["Column_Name"].ToString();
ddl.ID = "ddlFields" + i.ToString();
tcell.Controls.Add(ddl);
trow.Cells.Add(tcell);
table.Rows.Add(trow);
i++;
}
hidColumns.Value = (--i).ToString();
plhColumnMatch.Controls.Add(table);
plhWorksheets.Visible = false;
plhColumns.Visible = true;
On the page is a button and it OnClick event contains the following code:
for (int i = 0; i <= intColumns; i++)
{
DropDownList ddl1 = new DropDownList();
string strValue;
Button btn = (Button)Page.FindControl("Button1");
ddl1 = (DropDownList)plhColumnMatch.FindControl("ddlField s0");
strValue = ddl1.SelectedValue;
The last piece of code generates the error. I have tired referencing a control (a button) that was added at design time but I get the same. I have tried variations, such as:
strValue = (plhColumnMatch.FindControl("ddlFields0")as DropDownList).SelectedValue;
but I still get the same.
I am using VS2005 with sp1 and it using localhost/http not file
Thanks
Robert
|