0

Whenever I try to retrieve the previous page dropdownlist control in bedCount() mwethod it gives this exception while I rest assure you that the control which I am searching is very much present in the previous page. What is the reason for this? My code is given below :

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

1 Answers1

0

When you change selected item in DropDownList1, then each item of your arrays

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

will become a null because the page is recreated after each postback (even after changing dropdown list selected item)

On the first load of page you get your arrays filled because you fill them manually in Page_Load

ilyabreev
  • 588
  • 4
  • 20
  • You're welcome! Try to read something about ASP.NET WebForms page lifecycle. [link](http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx) for example – ilyabreev Mar 16 '13 at 05:29
  • OK. Based on your explaination I have changed my code and question. You can see the changed code in the question. Now I need to know the reason for this exception and how to access the controls from the previous page? – Suvo25 Mar 16 '13 at 11:12
  • I suggest, **PreviousPage** is null or result of **(DropDownList)PreviousPage.Master.FindControl("ContentPlaceHolder1")** is null – ilyabreev Mar 16 '13 at 17:39
  • And why is it null when the control is present in the previous page? – Suvo25 Mar 17 '13 at 04:59