Accessing server control from within app_code class
<p>My question is about accessing a server control (listbox) that is located in default.aspx.</p>

<p>I wish to access this control in Functions.cs (this class is located in the App_Code folder).</p>

<p>My page structure is:</p>

<ul>
<li>1 masterpage with 1 content holder</li>
<li>Default.aspx (all the controls are within the content place holder)</li>
<li>Functions.cs (located in App_Code)</li>
</ul>

<p>Now when I try to fill up the listbox elements I get the error <strong>"object reference not set to an instance of an object."</strong></p>

<p>What I have tried to gain access to this control: (this code is located in Functions.cs in App_Code).
This is basically showing some items  in the listbox that are located in an xml file</p>

<pre><code>private static string file = HttpContext.Current.Server.MapPath("~/App_Data/Questions.xml");

public static void ListItems()
{
    XmlDocument XMLDoc = new XmlDocument();
    XMLDoc.Load(file); 
    XPathNavigator nav = XMLDoc.CreateNavigator();

    XPathExpression expr;
    expr = nav.Compile("/root/file/naam");
    XPathNodeIterator iterator = nav.Select(expr);

    //ATTEMPT to get access to ServerControl(listbox)
    Page page = (Page)HttpContext.Current.Handler;
    ListBox test = (ListBox)page.FindControl("lbTest"); //control is called lbTest in Default.aspx

    test.Items.Clear();

    while (iterator.MoveNext())
    {
        test.Items.Add(iterator.Current.Value);
    }
}
</code></pre>

<p>Code from the default.apx file</p>

<pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/MasterFile.master" AutoEventWireup="true" CodeFile="default.aspx.cs" Inherits="default" Debug="true" %&gt;

&lt;%@ MasterType TypeName="Master" %&gt;

&lt;asp:Content ID="Content1" ContentPlaceHolderID="cphContent" Runat="Server" &gt;

    &lt;asp:MultiView ID="mvTest" runat="server" &gt;

        &lt;asp:View ID="vCollection" runat="server"&gt;
            &lt;asp:ListBox ID="lbTest" runat="server" CssClass="listbox" &gt;&lt;/asp:ListBox&gt;
        &lt;/asp:View&gt;


    &lt;/asp:MultiView&gt;

&lt;/asp:Content&gt;
</code></pre>

<p>The masterfile itself just has 1 placeholder.</p>

<p>Then I call upon the function ListItems in the Default.aspx.cs file</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
{    
    Functions.ListItems();
}
</code></pre>
