How do I fill in text on an ASP.NET page from a C# program?
<p>I am attempting to fill in an ASP.NET page textbox with some predefined text so that when it is displayed the value is predefined. I have tried</p>

<pre><code>protected void Page_PreRender ()
{
    mytextbox.Text = somestring;
}
</code></pre>

<p>which works fine in the development environment but on the server produces...</p>

<pre><code>System.NullReferenceException: Object reference not set to an instance of an object
</code></pre>

<p>The same applies when I try this in <code>Page_Load</code>. As I read the answers to <a href="http://stackoverflow.com/questions/5331968/how-do-i-set-an-asp-net-label-text-from-code-behind-on-page-load">this</a> question, what I am trying should work (in at least one of these places).</p>

<p>Can anyone see what I am doing wrong?</p>

<p><strong>EDIT</strong> more code, as suggested. The C# looks like this:-</p>

<pre><code>    protected void Page_PreRender (Object sender, EventArgs e)
    {
        try
        {
            string [] file_list;
            int i = 0;

            file_list = Directory.GetFiles(MyProg.Common.GetDirectory(),
                                           MyProg.Common.GetFileNameRoot() + "*.*");

            foreach (string filename in file_list)
            {
                string filenameonly = Path.GetFileName (filename);

                if (filenameonly == MyProg.Common.GetFileNameRoot() + "runlog.log")
                {
                    nametextbox.Text = filenameonly;
                }

            }

        }
        catch (Exception ex)
        {
            string mystring = ex.ToString();
            errorMessage.Text = "Page Load Error : " + mystring;
        }

    }
</code></pre>

<p>and the ASP.NET page like this...</p>

<pre><code>&lt;%@ Page Language="C#"
         AutoEventWireup="true"
         CodeBehind="MyDialogue.aspx.cs"
         Inherits="MyDialogue" %&gt;
&lt;%@ Register assembly="ComponentArt.Web.UI"
             namespace="ComponentArt.Web.UI"
             tagprefix="ComponentArt" %&gt;
&lt;%@ Register assembly="ComponentArt.Web.Visualization.Charting"
             namespace="ComponentArt.Web.Visualization.Charting"
             tagprefix="cc1" %&gt;

&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

&lt;html xmlns="http://www.w3.org/1999/xhtml" &gt;
&lt;head runat="server"&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id="myForm" runat="server"&gt;
&lt;div style="visibility:hidden"&gt;
&lt;asp:TextBox ID="nametextbox"
             TextMode="MultiLine"
             runat="server"
             Visible="true" /&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre>
