文本框值不会在asp.net中更新

时间:2014-08-29 17:05:13

标签: c# asp.net

我的页面中有一些文本框。我从数据库中获取值并在页面加载中更新文本框。我有一个代码来更新数据库后面的更新按钮的值。问题是当我更改文本框值并单击更新按钮时,文本框中的值也是原始值。它保留了它的价值。我的代码是

protected void Page_Load(object sender, EventArgs e)
    {
        int id = Convert.ToInt32(Session["DocumentID"]);
        Connection conn = new Connection();
        string query = "SELECT * from Document where DocumentID='" + id + "'";
        SqlCommand sqlcom = new SqlCommand(query, conn.conopen());
        SqlDataAdapter daexp = new SqlDataAdapter(sqlcom);
        System.Data.DataTable dtexp = new System.Data.DataTable();
        daexp.Fill(dtexp);
        TextBox1.Text = dtexp.Rows[0][1].ToString();
        TextBox3.Text = dtexp.Rows[0][2].ToString();
        TextBox6.Text = dtexp.Rows[0][3].ToString();
        TextBox4.Text = dtexp.Rows[0][4].ToString();
        TextBox5.Text = dtexp.Rows[0][5].ToString();
        TextBox7.Text = dtexp.Rows[0][6].ToString();
        TextBox7.ReadOnly = true;
    }



protected void Button1_Click(object sender, EventArgs e)
    {
    int id = Convert.ToInt32(Session["DocumentID"].ToString());
    if (FileUpload1.HasFile)
    {
        HttpPostedFile Image = FileUpload1.PostedFile;
        string contentType = Image.ContentType;
        if (contentType.Equals("application/octet-stream"))
        {
            contentType = "application/pdf";
        }
        else if (contentType.Equals("application/vnd.openxmlformats-officedocument.wordprocessingml.document"))
        {
            contentType = "application/msword";
        }
        int nFileLen = Image.ContentLength;
        byte[] myData = new byte[nFileLen];
        Image.InputStream.Read(myData, 0, nFileLen);
        Connection con = new Connection();
        con.conopen();
        string query = "UPDATE Document SET Setup='" + TextBox1.Text + "', ReferenceNO='" + TextBox3.Text + "', DocumentDate='" + TextBox6.Text + "', Subject='" + TextBox4.Text + "', NameOfInitiator='" + TextBox5.Text + "', Document=@Doc, FolderID='" + DropDownList1.Text + "', DocTypeID='" + DropDownList4.Text + "', DirectorateID='" + DropDownList3.Text + "', OrganizationID='" + DropDownList2.Text + "' WHERE DocumentID='" + id + "'";
        SqlCommand sqlcom = new SqlCommand(query, con.conopen());
        sqlcom.Parameters.AddWithValue("@Doc", myData);
        sqlcom.ExecuteNonQuery();
        Label12.Text = "Document Updated Successfully";
        Label12.ForeColor = System.Drawing.Color.Green;
        Label12.Visible = true;
        Label12.Text = "Document Updated Successfully";
        Label12.ForeColor = System.Drawing.Color.Green;
        Label12.Visible = true;
    }
    else
    {
        Connection conn = new Connection();
        string query = "UPDATE Document SET Setup='" + TextBox1.Text + "', ReferenceNO='" + TextBox3.Text + "', DocumentDate='" + TextBox6.Text + "', Subject='" + TextBox4.Text + "', NameOfInitiator='" + TextBox5.Text + "', FolderID='" + DropDownList1.Text + "', DocTypeID='" + DropDownList4.Text + "', DirectorateID='" + DropDownList3.Text + "', OrganizationID='" + DropDownList2.Text + "' WHERE DocumentID='" + id + "'";
        SqlCommand sqlcom = new SqlCommand(query, conn.conopen());
        sqlcom.ExecuteNonQuery();
    }
  }

3 个答案:

答案 0 :(得分:1)

您正在Page_Load更新数据库中文本框的值。每次回发页面时,它都会从数据库获取值并在TextBoxes中设置。如果第一次加载页面而不回发页面,则需要添加一个检查并仅更新值并设置为TextBoxes。像这样:

protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostBack)
{
//your code here...
}
}

答案 1 :(得分:0)

我知道:asp.net的订单事件PreInit -Init -InitComplete -PreLoad - LoadPage - 控制事件 -LoadComplete -PreRender -SaveStateComplete -Render -Unload

因此,当你onckick按钮时,网页将会后退。之后,调用pageload - > EventButton。文本框的值更改,因为 pageload 设置了文本框的值。

修改代码:

protected void Page_Load(object sender, EventArgs e)
    {
if(!Page.IsPostBack){
    int id = Convert.ToInt32(Session["DocumentID"]);
    Connection conn = new Connection();
    string query = "SELECT * from Document where DocumentID='" + id + "'";
    SqlCommand sqlcom = new SqlCommand(query, conn.conopen());
    SqlDataAdapter daexp = new SqlDataAdapter(sqlcom);
    System.Data.DataTable dtexp = new System.Data.DataTable();
    daexp.Fill(dtexp);
    TextBox1.Text = dtexp.Rows[0][1].ToString();
    TextBox3.Text = dtexp.Rows[0][2].ToString();
    TextBox6.Text = dtexp.Rows[0][3].ToString();
    TextBox4.Text = dtexp.Rows[0][4].ToString();
    TextBox5.Text = dtexp.Rows[0][5].ToString();
    TextBox7.Text = dtexp.Rows[0][6].ToString();
    TextBox7.ReadOnly = true;

}     }

答案 2 :(得分:0)

尝试引用值属性:

TextBox1.Attributes("value")