0

bedCount() mwethod で前のページのドロップダウン リスト コントロールを取得しようとすると、常にこの例外が発生しますが、検索しているコントロールが前のページに非常に多く存在することを保証します。これの理由は何ですか?私のコードは以下のとおりです:

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";
}
4

1 に答える 1

0

DropDownList1 で選択した項目を変更すると、配列の各項目

DropDownList[] adult=new DropDownList[6];
DropDownList[] child = new DropDownList[6];

各ポストバック後にページが再作成されるため (ドロップダウン リストの選択した項目を変更した後でも) 、 nullになります。

ページの最初のロードでは、 Page_Loadで手動で配列を埋めるため、配列が埋められます。

于 2013-03-15T18:44:21.187 に答える