在 asp.net 将文本保存到数据库,然后通过加载页面调用它不起作用,为什么

本文关键字:加载 调用 为什么 不起作用 然后 net asp 文本 保存 数据库 | 更新日期: 2023-09-27 18:33:23

my in program首先从 datadase 调用文本,然后将其发布到文本框中其次对文本中的文本进行更改最后我需要数据库的更改.当我单击保存按钮时出现问题,不会在文本框中进行更改,而仅在数据库上进行更改

namespace WebApplication4
{
    public partial class WebForm17 : System.Web.UI.Page
    {
         DATAACCESS aaa = new DATAACCESS();
         string  Dname = "Finding";
         protected void Page_Load(object sender, EventArgs e)
         {
              aaa = new DATAACCESS();
              TextBox2.Text= aaa.GetDoctext(Dname, 90);
         }
         protected void Button5_Click(object sender, EventArgs e)
         {
             string textboxvalue = Request.Form[TextBox2.UniqueID];
             aaa.savetext(textboxvalue, Dname, 90);
         }
    }
}

在 asp.net 将文本保存到数据库,然后通过加载页面调用它不起作用,为什么

您需要

了解 ASP.NET 页面生命周期 - page_load将在任何按钮事件之前执行。

这意味着只有在尝试从数据库加载,您才会保存到数据库。

您应该做的是仅在页面尚未回发时才填充文本框:

namespace WebApplication4 
{ 
  public partial class WebForm17 : System.Web.UI.Page 
  { 
     DATAACCESS aaa = new DATAACCESS(); 
     string  Dname = "Finding"; 
     protected void Page_Load(object sender, EventArgs e) 
     {  
          aaa = new DATAACCESS(); 
          if(!IsPostBack)
          {
              TextBox2.Text= aaa.GetDoctext(Dname, 90); 
          }
     } 
     protected void Button5_Click(object sender, EventArgs e) 
     { 
         string textboxvalue = TextBox2.Text;
         aaa.savetext(textboxvalue, Dname, 90); 
     } 
  } 
}