c#文本框设置值并编辑不保存

c#文本框设置值并编辑不保存,c#,asp.net,C#,Asp.net,我在Textbox上有一个,我从db加载文本,然后编辑并保存,但当我在BindTextBox中设置TextBox2.text,并完成编辑后,它在InsertToDatabase中的值与BindTextBox方法中的值相同,它不符合我是否编辑过它 protected void Page_Load(object sender, EventArgs e) { BindTextBox } protected void BindTextBox() {

我在Textbox上有一个,我从db加载文本,然后编辑并保存,但当我在BindTextBox中设置TextBox2.text,并完成编辑后,它在InsertToDatabase中的值与BindTextBox方法中的值相同,它不符合我是否编辑过它

protected void Page_Load(object sender, EventArgs e)
    {
        BindTextBox
    }


    protected void BindTextBox()
    {
        SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString);
        SqlCommand myCommand = new SqlCommand("dbo.Search", myConnection);
        string message = "";
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Parameters.Add("@ID", SqlDbType.NVarChar, 20).Value = Utility.qID;
        myConnection.Open();
        SqlDataReader reader = myCommand.ExecuteReader();
        while (reader.Read())
        {
            message = reader["Description"].ToString();            
        }
        myConnection.Close();
        TextBox2.Text = message;
    }

    protected void InsertToDatabse()
    {
        string text = TextBox2.Text.ToString();
        SqlConnection myConnection = new SqlConnection(Globals.DatabaseConnectionString);
        SqlCommand myCommand = new SqlCommand("dbo.Insert", myConnection);
        myCommand.CommandType = CommandType.StoredProcedure;
        myCommand.Parameters.Clear();
        myCommand.Parameters.Add("@ID", SqlDbType.NVarChar, 20).Value = Utility.qID;
        myCommand.Parameters.Add("@Description", SqlDbType.NVarChar, 200).Value = text;
        myConnection.Open();
        myCommand.ExecuteNonQuery();
        myConnection.Close();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        InsertToDatabse();
        BindTextBox();
    }

您应该检查您的页面是否已回发

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
      BindTextBox();
}

当您按下“保存”按钮时,回发到服务器的操作完成,并再次调用加载方法。这会导致您的textbox值被覆盖,因为您在初始加载和回发时都调用了BindTextBox

在pageload上调用Bindtextbox()之前,请检查回发