根据数据库值更改控件

时间:2012-06-19 13:41:37

标签: c# asp.net

问题:

我在数据库表中有一个值。此值可以包含数字,也可以为null。如果它为null我想显示一组控件。如果它不为null我想展示另一组控件。


以前的尝试:

我尝试在代码中创建控件,具体取决于数据库的值。这很有效。但是,在回发时我得到一个空引用异常。回发中不存在该控件,因为该页面是无状态的。我正在page_load处理程序中构建控件(取决于表列的值)。由于我在page_load中创建控件,它们不应该在回发中存在吗?

我还尝试在按钮的事件处理程序中重新创建控件。我得到一个“theres已经是一个具有此id的控件”异常(大概是因为我已经在page_load方法中创建了它。)

我读了一些关于如何在会话中存储控件的帖子。这看起来比应该做的更多。

问题:

  1. 我是以错误的方式来做这件事的吗?这似乎应该很简单但是变得一团糟。
  2. 如果这是正确的方法,我在哪里添加会话信息?我一直在阅读其他帖子,我有点迷失
  3. 代码:

        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);
        }
    

1 个答案:

答案 0 :(得分:3)

看起来您正在根据数据库值创建一组静态控件。为什么不简单地让2个Panel包含您想要的控件,只需将其visibility设置为true或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;
    }
}