I’m trying to access a forms control from a class file in the App_Code directory but I keep getting the message “Object reference not set to an instance of an object.”
Not really sure where I’m failing here but this is the code:
public static void openPage(Page page)
{
DropDownList eventType = (DropDownList)page.FindControl("eventType");
if (eventType.SelectedItem.Text == "Big Party")
{
DoSomeWork(); //should be changing values or visible options on the page
}
}
And I’m calling it from my page like so:
Workflow.openPage(this);
It keeps telling my that the issue is from: if (eventType.SelectedItem.Text == "Big Party")
Any ideas?
The reason you get the “Object reference not set to an instance of an object.” is because FindControl is returning null, and you are trying to derefrence a null reference. If you place a check for null before the code, it will stop throwing the exception.
Of course this won’t give you want you’re looking for.
Where are you calling Workflow.openPage from? More than likely, you’re calling it from too early in the page lifecycle and the control hasn’t been created yet.