NullReferenceException,即使控件存在于前一页
本文关键字:存在 于前 一页 控件 NullReferenceException | 更新日期: 2023-09-27 18:04:02
每当我尝试在bedCount()方法中检索上一页下拉列表控件时,它会给出此异常,而我向您保证,我正在搜索的控件非常存在于上一页中。这是什么原因呢?我的代码如下:
public partial class Room2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
DropDownList[] adult=new DropDownList[6];
DropDownList[] child = new DropDownList[6];
TableRow[] trp = new TableRow[5];
TableRow[] trc ={ trc1, trc2, trc3, trc4, trc5 };
DropDownList[] rtype ={ DropDownList1,DropDownList2, DropDownList3, DropDownList4, DropDownList5, DropDownList6 };
Label[] bed ={Label1,Label2,Label3,Label4,Label5,Label6};
int i,x,c=2;
for (i = 0; i < 5; i++)
{
trp[i] = (TableRow)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("tr" + (i + 1));
}
for (i = 0; i < 6; i++,c+=4)
{
adult[i] = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList" + c++);
child[i] = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList" + c);
}
for (i = 0; i < 6; i++)
{
x = adult[i].SelectedIndex + child[i].SelectedIndex;
switch (x)
{
case 0:
case 1:
rtype[i].Items.Add("Executive Class");
rtype[i].Items.Add("Business Class");
rtype[i].Items.Add("Gold Class (Type-I)");
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "No";
break;
case 2:
rtype[i].Items.Add("Business Class");
rtype[i].Items.Add("Gold Class (Type-I)");
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "1";
break;
case 3:
bed[i].Text = "1";
goto case 4;
case 4:
rtype[i].Items.Add("Gold Class (Type-I)");
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "2";
break;
case 5:
bed[i].Text = "2";
goto case 6;
case 6:
rtype[i].Items.Add("Gold Class (Type-II)");
rtype[i].SelectedIndex = 0;
bed[i].Text = "3";
break;
}
if (i<5 && trp[i].Visible)
trc[i].Visible = true;
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if(DropDownList1.Items.Count<=3)
bedCount(DropDownList1,Label1,0);
}
protected void bedCount(DropDownList d,Label l,int a)
{
protected void bedCount(DropDownList d,Label l,int x)
{
DropDownList a=new DropDownList();
DropDownList c = new DropDownList();
int s;
a = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList"+x++);//gives exception here
c = (DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1").FindControl("DropDownList"+x);
s = c.SelectedIndex + c.SelectedIndex;
if (d.SelectedItem.Equals("Business Class"))
if(s==2)
l.Text = "1";
else
l.Text = "No";
else if(d.SelectedItem.Equals("Gold Class (Type-I)"))
if(s==3)
l.Text = "1";
else if(s==4)
l.Text = "2";
else
l.Text = "No";
else if(d.SelectedItem.Equals("Gold Class (Type-II)"))
if(s==4)
l.Text = "1";
else if(s==5)
l.Text = "2";
else if(s==6)
l.Text = "3";
else
l.Text = "No";
}
当您更改DropDownList1中的选定项时,则数组中的每个项
DropDownList[] adult=new DropDownList[6];
DropDownList[] child = new DropDownList[6];
将成为null,因为每次回发后都会重新创建页面(即使在更改下拉列表所选项之后)
在第一次加载页面时,数组被填充因为你手动填充它们在Page_Load