且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

SQL Server:更新不使用 C# 更新数据库

更新时间:2023-02-08 17:05:19

ASP.NET 页面生命周期 如果您不保护 Page_Load 不重新执行填充文本框的代码,则会导致这种情况.

ASP.NET page lifecycle causes this situation if you don't protect the Page_Load from reexecuting the code that fill your textboxes.

if (!IsPostBack)
{
    string selectStatement = "SELECT * FROM ahu_data WHERE unit_ID = " + reportID;
    string sqlConnectionString = "Removed for Security";
    using (SqlConnection connection1 = new SqlConnection(sqlConnectionString))
    {
      .... rest of code that pre-fill your fields

页面.IsPostBack 是 Page 的布尔属性,它会通知您的代码是第一次调用该页面还是由于某些需要在服务器端处理的事件而调用该页面.
在后一种情况下,您不应再次执行填充文本框的代码,否则,当流程到达您的按钮代码时,您将找到具有原始值的文本框,而不是修改后的文本框,因为 Page_Load 有重置了一切.

Page.IsPostBack is a boolean property of the Page that informs your code if the page is called for the first time or if it is called as a consequence of some event that need to be processed server-side.
In the latter case you should not execute again the code that fills your textboxes otherwise, when the flow reaches your button code, you will find the textboxes with the original values instead of the modified ones because the Page_Load has resetted everything.

不要忘记上面关于参数化您的第一个查询的评论.您已经完成了对 UPDATE 进行参数化的大部分工作,只剩下一个参数就完成了.

And do not forget the comment above about parameterizing your first query. You have already done the biggest part parameterizing the UPDATE, just one parameter remains and it is complete.