string r = (string)(" ");
r = Session["Region"].ToString(); // stepping shows r = "Region 1"
if (IsPostBack)
{
if (Session["Region"] != null)
{
MasterPage myMaster = Master as MasterPage;
DropDownList regionDropDownList = (DropDownList)myMaster.FindControl("cboRegion");
regionDropDownList.Text = Session["Region"].ToString(); // <--- NullReferenceException **********
}
}
private Control RecursiveFind(Control root, string id)
{
if (root.ID == id)
return root;
Control c = null;
foreach (Control ctrl in root.Controls)
{
c = RecursiveFind(ctrl);
if (c != null)
break;
}
return c;
}
Panel p = (Panel)Master.FindControl("A");
DropDownList ddl = (DropDownList)p.FindControl("B");
MasterPage myMaster = Master as MasterPage;
TableCell td = (TableCell)Master.FindControl("Region");
DropDownList ddl = (DropDownList)td.FindControl("cboRegion");
ddl.Text = Session["Region"].ToString();
string r = (string)(" ");
r = Session["Region"].ToString(); // stepping shows r = "Region 1"
if (IsPostBack)
{
if (Session["Region"] != null)
:
:
string r = (string)Session["Region"];
To resolve the error:
if (regionDropDownList != null)
regionDropDownList.Text = Session["Region"].ToString
To find the drop down correctly:
The find control isn't recursive.
You need to do the find control on cboRegion's parent or create a recursive find.