我正在尝试创建由数据库字段确定的动态控件,当控件实际出现时,它们会抛出一个空引用错误。
在此段的最后一行代码中抛出错误。我可以看到数组被实例化为类型文本框,但是[0]位置(以及随后的所有内容)都是null。我错过了什么?
var query = from a in com.Communications
where a.fddkey == res.FDDKey
select a;
var querydd = from b in com.Communications
select b.subject;
int maxrows = query.Count();
TextBox[] notestb = new TextBox[maxrows * 2];
DropDownList[] notesdd = new DropDownList[maxrows];
int i = 0;
foreach (var s in query)
{
TableRow tr = new TableRow();
tr.Cells.Add(new TableCell());
tr.Cells.Add(new TableCell());
tr.Cells[0].Text = "Date:";
tr.Cells[1].Text = "Correspondent:";
this.tblnotes.Rows.Add(tr);
TableRow tr2 = new TableRow(); // Not sure if new row needs to be instantiated or not.
tr2.Cells.Add(new TableCell());
tr2.Cells.Add(new TableCell());
notestb[i].ID = "notestb" + i.ToString();
答案 0 :(得分:1)
数组notestb
本身不是null,但是你没有实例化其中的任何元素,这就是它们都为空的原因。
notestb[i] = new TextBox();
notestb[i].ID = "notestb" + i.ToString();
答案 1 :(得分:1)
您没有实例化notestb
元素:
TextBox[] notestb = new TextBox[maxrows * 2];
foreach(var t in notestb )
t = new TextBox();