Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change controls based on database value

Tags:

c#

asp.net

Problem:

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.


Previous Attempts:

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 page_load handler (depending on the value of the table column). Since I'm creating the controls in the page_load shouldn't they exist on postback?

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 page_load method).

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.

Questions:

  1. Am I going about this the wrong way? This seems like it should have been simple but is turning into a mess.
  2. 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

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 > 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);
    }
like image 323
Steve's a D Avatar asked Sep 22 '26 18:09

Steve's a D


1 Answers

It looks like you're creating a static set of controls based on the database value. Why not simply have 2 Panels that contain the controls you want and simply set their visibility to true or false:

if (!Page.IsPostBack)
{
    if (int.TryParse(Request.QueryString["id"], out bookId))
    {
        empName = Book.GetCopyOwner(bookId, Request.QueryString["owner"]);
        var display = (empName.Trim().Length > 0);

        panelReturnControls.Visible = display;
        panelCheckoutControls.Visible = !display;
    }
}
like image 56
CAbbott Avatar answered Sep 25 '26 08:09

CAbbott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!