textBoxName.Text 没有获取用户输入的文本,只有加载时设置的文本

textBoxName.Text isn't getting the text entered by user, only the text that was set there on load

我有这个文本框:

<asp:TextBox ID="txtDescription" runat="server" Height="90%" TextMode="MultiLine" Width="90%"></asp:TextBox>

我设置了它的文本值:

description = reader["Description"].ToString();
txtDescription.Text = description;

这是为了编辑 "description",所以我在框中有现有的描述,然后想要获取框中末尾的文本,因为它已被更改。

然而我这样做:

 string newDecription = txtDescription.Text;

并且该值是它最初设置的值。上面获取文本值的代码是 运行 在提交按钮上,所以不是 运行 并在编辑之前获取值。

上下文

 protected void Page_Load(object sender, EventArgs e)
    {
        getValues();
    }
string ID = 1;
protected void getValues()
    {
        using (SqlConnection conn = new SqlConnection(connString))
        using (SqlCommand comm = new SqlCommand("SELECT Name,Stock,Price250g,Price1kg,Description,StockOrdered FROM Stock WHERE id = @ID", conn))
        {
            comm.Parameters.AddWithValue("@ID", ID);
            conn.Open();
            using (SqlDataReader reader = comm.ExecuteReader())
            {
                while (reader.Read())
                {
                    price250g = reader["Price250g"].ToString();
                    price1kg = reader["Price1kg"].ToString();
                    name = reader["Name"].ToString();
                    description = reader["Description"].ToString();
                    stock = reader["Stock"].ToString();
                    stockOrdered = Convert.ToBoolean(reader["StockOrdered"].ToString());

                    lblName.Text = name;
                    lbl250g.Text += price250g.Remove(price250g.Length - 2);
                    lbl1kg.Text += price1kg.Remove(price1kg.Length - 2);
                    lblStock.Text = stock + "g";
                    cbStockOrdered.Checked = stockOrdered;
                    txtDescription.Text = description;
                }
            }
        }
    }


private void addAddressToOrder()
    {
        using (SqlConnection conn = new SqlConnection(connString))
        using (SqlCommand cmd = new SqlCommand("UPDATE Stock SET Name=@name, Stock=@stock, Price250g=@price250, Price1kg=@price1kg, Description=@description, StockOrdered=@ordered WHERE Id=@ID", conn))
        {
            cmd.Parameters.AddWithValue("@ID", ID);
            cmd.Parameters.AddWithValue("@description", txtDescription.Text);

            cmd.Parameters.AddWithValue("@name", txtName.Text);
            cmd.Parameters.AddWithValue("@price250", txtPrice250g.Text);
            cmd.Parameters.AddWithValue("@price1kg", txtPrice1kg.Text);

            stock = ddAddStock.SelectedValue + stock;
            cmd.Parameters.AddWithValue("@stock", stock);
            cmd.Parameters.AddWithValue("@ordered", cbStockOrdered.Checked);

            conn.Open();
            cmd.ExecuteNonQuery();
        }
    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        addAddressToOrder();
        Response.Redirect("~/Admin/AdminHome.aspx");
    }

当您这样做时通常会发生这种情况:

protected void Page_Load(object sender, EventArgs e)
{
    // other code
    txtDescription.Text = description;
}

原因是因为page lifecycle eventsPage_Load 每次 页面加载时执行 。这包括 post-backs。 (毕竟,您需要加载页面才能与之交互。)它 运行s 任何按钮处理程序或其他类似的控件事件之前。因此,如果这段向文本框写入值的代码在您从文本框中读取值之前执行,那么它将覆盖该值。

Page_Load 中的任何代码(或类似的页面初始化事件)不应 运行 在 post-backs 上需要包含在条件中:

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        // other code
        txtDescription.Text = description;
    }
}