change controls based on database value
<p><strong>Problem:</strong></p>

<p>I have a value in a database table. This value can either contain a number, or null. If its null I would like to show one group of controls. If its not null I would like to show another group of controls.</p>

<hr>

<p><strong>Previous Attempts:</strong></p>

<p>I have tried creating the controls in the code behind depending on the value of the database. This worked. However, on postback I get a null reference exception. The control doesn't exist on postback because the page is stateless. I'm building the controls in the <code>page_load</code> handler (depending on the value of the table column). Since I'm creating the controls in the <code>page_load</code> shouldn't they exist on postback?</p>

<p>I also tried recreating the controls in the event handler for the button. I get a "theres already a control with this id" exception (presumably because I already created it in the <code>page_load</code> method).</p>

<p>I read a few posts about how I have to store the controls in a session. This seems like more work than it should be. </p>

<p><strong>Questions:</strong></p>

<ol>
<li>Am I going about this the wrong way? This seems like it should have been simple but is turning into a mess.</li>
<li>If this is the correct way to do this, Where do I add the session information? I've been reading other posts and I'm kind of lost</li>
</ol>

<p><strong>Code:</strong></p>

<pre><code>    int bookId;
    string empName;

    protected void Page_Load(object sender, EventArgs e)
    {
        if(int.TryParse(Request.QueryString["id"], out bookId))
        {
            //This is where the value in the database comes into play. If its null Book.GetCopyOwner
            // returns a string with length 0

            empName = Book.GetCopyOwner(bookId, Request.QueryString["owner"]);
            if (empName.Trim().Length &gt; 0)
            {
                CreateReturnControls();
            }
            else
            {
                CreateCheckoutControls();
            }
        }
    }

    protected void ReturnButton_Click(object sender, EventArgs e)
    {
    }

    protected void CheckOut_Click(object sender, EventArgs e)
    {
        int bookId;
        if (int.TryParse(Request.QueryString["id"], out bookId))
        {
            TextBox userId = (TextBox)this.Page.FindControl("UserId");

            //WHEN I TRY TO USE THE TEXTBOX userId HERE, I GET NULL REFERENCE EXCEPTION

            BookCopyStatusNode.Controls.Clear();
            CreateReturnControls();
        }
    }

    protected void CopyUpdate_Click(object sender, EventArgs e)
    {
    }

    private void CreateCheckoutControls()
    {
        TextBox userId = new TextBox();
        //userId.Text = "Enter Employee Number";
        //userId.Attributes.Add("onclick", "this.value=''; this.onclick=null");
        userId.ID = "UserId";

        Button checkOut = new Button();
        checkOut.Text = "Check Out";
        checkOut.Click += new EventHandler(CheckOut_Click);

        TableCell firstCell = new TableCell();
        firstCell.Controls.Add(userId);

        TableCell secondCell = new TableCell();
        secondCell.Controls.Add(checkOut);

        BookCopyStatusNode.Controls.Add(firstCell);
        BookCopyStatusNode.Controls.Add(secondCell);
    }

    private void CreateReturnControls()
    {
        Label userMessage = new Label();
        userMessage.Text = empName + " has this book checked out.";

        Button returnButton = new Button();
        returnButton.Text = "Return it";
        returnButton.Click += new EventHandler(ReturnButton_Click);

        TableCell firstCell = new TableCell();
        firstCell.Controls.Add(userMessage);

        TableCell secondCell = new TableCell();
        secondCell.Controls.Add(returnButton);

        BookCopyStatusNode.Controls.Add(firstCell);
        BookCopyStatusNode.Controls.Add(secondCell);
    }
</code></pre>
