C# 在运行时将web控件添加到asp.net表
我希望在运行时以固定的计时器间隔将asp.net文本web控件添加到表中。控件将从作为数组的类的实例中获取静态值 我不是OOP爱好者,但据我所知,我需要声明这个类一次,然后继续引用它存储的值,因为我需要这些值为web控件提供文本值 下面是我一直得到的null引用异常抛出,尽管初始化的不仅仅是作为数组的类,而是类数组中的每个项 为什么? 编辑: 下面是类声明和初始化函数:C# 在运行时将web控件添加到asp.net表,c#,asp.net,web-services,C#,Asp.net,Web Services,我希望在运行时以固定的计时器间隔将asp.net文本web控件添加到表中。控件将从作为数组的类的实例中获取静态值 我不是OOP爱好者,但据我所知,我需要声明这个类一次,然后继续引用它存储的值,因为我需要这些值为web控件提供文本值 下面是我一直得到的null引用异常抛出,尽管初始化的不仅仅是作为数组的类,而是类数组中的每个项 为什么? 编辑: 下面是类声明和初始化函数: public class Names { String name = ""; public String Name {
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);
*/
}
}
}
protectedvoid Timer1\u Tick(对象发送方,事件参数e)
{
表1.Rows.Clear();
对于(int i=0;i<10;i++)
{
尝试
{
if(!\u yahoo[i]。初始化的| |!\u名称[i]。初始化的| |!\u基[i]。初始化的)
{
名称初始化(_yahoo);
名称初始化(_名称);
名称初始化(_base);
}
其他的
{
Yahoo_Load(_Yahoo[i]);
//
TableRow _table_row=新TableRow();
表1.行。添加(_table_row);
//
TableCell _cell_1=新的TableCell();
_cell_1.Text=_names[i].Name;
_表_行.单元格.添加(_单元格_1);
//
TableCell _cell_2=新的TableCell();
_cell_2.Text=Convert.ToString(_base[_names[i].Index].Basis);
_表_行.单元格.添加(_单元格_2);
//
TableCell _cell_3=新的TableCell();
_单元格3.Text=\u名称[i]。引号;
_表_行.单元格.添加(_单元格_3);
//
}
}
抓住
{
//故意将其注释掉,并引发空异常错误。
//情况不应该如此。
/*
名称初始化(_yahoo);
名称初始化(_名称);
名称初始化(_base);
*/
}
}
}
您的函数名称初始化错误
public static void NamesInitialise(Names[] names)
{
names = new Names[10];
您正在内部创建数组,该数组不替换传递的变量
改为
public static Names[] NamesInitialise()
{
var names = new Names[10];
.....
return names;
}
除此之外,在循环内部迭代之前初始化数组
更重要的是,您正在使用单个勾号填充所有行,这可能是错误的,这里没有渐进添加行
而且,表不会保留在上一次回发中添加的行/单元格