在运行时将web控件添加到asp.net表中
本文关键字:asp net 表中 添加 控件 运行时 web | 更新日期: 2023-09-27 18:28:34
我想在运行时以定时间隔将asp.net文本web控件添加到表中。控件将从数组类的实例中获取静态值。
我不是OOP爱好者,但据我所知,我需要声明一次这个类,然后继续引用它存储的值,因为我需要这些值来为web控件提供文本值。
以下是我不断抛出的null引用异常,尽管我不仅将类初始化为数组,还将类的数组中的每个项初始化。
为什么?
编辑:
以下是类声明和初始化函数:
public class Names
{
String name = ""; public String Name { get { return name; } set { name = value; } }
Byte name_id = 0; public Byte NameID { get { return name_id; } set { name_id = value; } }
Decimal point = 0; public Decimal Point { get { return point; } set { point = value; } }
Int64 index = 0; public Int64 Index { get { return index; } set { index = value; } }
String quote = ""; public String Quote { get { return quote; } set { quote = value; } }
Boolean initialised = false; public Boolean Initialised { get { return initialised; } set { initialised = value; } }
}
public static Names[] _names;
public static void NamesInitialise(Names[] names)
{
names = new Names[10];
//
names[0] = new Names();
names[0].Name = "LOSANGELES";
names[0].NameID = 14;
names[0].Index = 0;
names[0].Point = Convert.ToDecimal(0.00001);
names[0].Initialised = true;
//
names[1] = new Names();
names[1].Name = "STOCKHOLM";
names[1].NameID = 15;
names[1].Index = 1;
names[1].Point = Convert.ToDecimal(0.001);
names[1].Initialised = true;
//
names[2] = new Names();
names[2].Name = "BERLIN";
names[2].NameID = 21;
names[2].Index = 2;
names[2].Point = Convert.ToDecimal(0.00001);
names[2].Initialised = true;
//
names[3] = new Names();
names[3].Name = "BONN";
names[3].NameID = 23;
names[3].Index = 3;
names[3].Point = Convert.ToDecimal(0.00001);
names[3].Initialised = true;
//
names[4] = new Names();
names[4].Name = "PARIS";
names[4].NameID = 24;
names[4].Index = 4;
names[4].Point = Convert.ToDecimal(0.00001);
names[4].Initialised = true;
//
names[5] = new Names();
names[5].Name = "TELAVIV";
names[5].NameID = 25;
names[5].Index = 5;
names[5].Point = Convert.ToDecimal(0.001);
names[5].Initialised = true;
//
names[6] = new Names();
names[6].Name = "ROME";
names[6].NameID = 31;
names[6].Index = 6;
names[6].Point = Convert.ToDecimal(0.00001);
names[6].Initialised = true;
//
names[7] = new Names();
names[7].Name = "NEWYORK";
names[7].NameID = 34;
names[7].Index = 7;
names[7].Point = Convert.ToDecimal(0.00001);
names[7].Initialised = true;
//
names[8] = new Names();
names[8].Name = "LONDON";
names[8].NameID = 35;
names[8].Index = 8;
names[8].Point = Convert.ToDecimal(0.001);
names[8].Initialised = true;
//
names[9] = new Names();
names[9].Name = "TOKYO";
names[9].NameID = 45;
names[9].Index = 9;
names[9].Point = Convert.ToDecimal(0.001);
names[9].Initialised = true;
//
}
public class Base : Names
{
Decimal basis = 0; public Decimal Basis { get { return basis; } set { basis = value; } }
}
public static Base[] _base;
这就是我在定时上使用它们的方式
protected void Timer1_Tick(object sender, EventArgs e)
{
Table1.Rows.Clear();
for (int i = 0; i < 10; i++)
{
try
{
if (!_yahoo[i].Initialised||!_names[i].Initialised||!_base[i].Initialised)
{
NamesInitialise(_yahoo);
NamesInitialise(_names);
NamesInitialise(_base);
}
else
{
Yahoo_Load(_yahoo[i]);
//
TableRow _table_row = new TableRow();
Table1.Rows.Add(_table_row);
//
TableCell _cell_1 = new TableCell();
_cell_1.Text = _names[i].Name;
_table_row.Cells.Add(_cell_1);
//
TableCell _cell_2 = new TableCell();
_cell_2.Text = Convert.ToString(_base[_names[i].Index].Basis);
_table_row.Cells.Add(_cell_2);
//
TableCell _cell_3 = new TableCell();
_cell_3.Text = _names[i].Quote;
_table_row.Cells.Add(_cell_3);
//
}
}
catch
{
//Commented this out on purpose and null exception errors are thrown.
//That should not be the case.
/*
NamesInitialise(_yahoo);
NamesInitialise(_names);
NamesInitialise(_base);
*/
}
}
}
您的函数NamesInitialize是错误的。
public static void NamesInitialise(Names[] names)
{
names = new Names[10];
您正在内部创建数组,该数组不会替换传递的变量。
更改为
public static Names[] NamesInitialise()
{
var names = new Names[10];
.....
return names;
}
除此之外,在循环内部迭代之前初始化数组。
此外,您正在用一个勾号填充所有行,这可能是错误的,这里没有渐进添加行。
此外,表不保留在以前的回发中添加的行/单元格