asp.net中没有更新文本框值

本文关键字:文本 更新 net asp | 更新日期: 2023-09-27 18:19:57

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

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

asp.net中没有更新文本框值

您正在更新Page_Load中数据库中Textboxes的值。每次页面返回时,它都会从数据库中获取值,并在TextBoxes中进行设置。若页面是第一次加载而并没有返回,则需要添加一个复选框,并且只更新值并设置为TextBoxes。像这样:

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

我知道:asp.net的订单事件PreInit-Init-InitialComplete-PreLoad-LoadPage-Control events-LoadComplete-预渲染-SaveStateComplete-渲染-卸载

因此,当你按下按钮时,网页将返回。之后,调用pageload->EventButton。textbox的值更改,因为您的页面加载为textbox设置了值。

编辑代码:

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;

}}

尝试引用值Attribute:

TextBox1.Attributes("value")