[英]Updated textbox value can't be reached by code
在此代碼中,我從數據庫讀取了患者信息,並帶有隨URL傳遞的PatientId參數。 然后,我在Page_Load中使用此值填充5個文本框。 但是在Update_Click處 ,代碼無法達到更新的文本框值,它僅讀取文本框的初始值。 我在調試時看到了它。 在數據庫級別沒有問題。 當我刪除Page_Load代碼並將文本框最初保留為空時, Update_Click中的代碼可以到達用戶輸入並更新值。 問題是什么?
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class admin_Testadmin : System.Web.UI.Page
{
Patient patient = new Patient();
protected void Page_Load(object sender, EventArgs e)
{
String cs = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (MySqlConnection cn = new MySqlConnection(cs))
{
MySqlCommand command = new MySqlCommand(StoredProcedures.select_patient, cn);
command.CommandType = CommandType.StoredProcedure;
int pid = int.Parse(Request.QueryString["patientId"]);
command.Parameters.AddWithValue("pid", pid);
command.Parameters["pid"].Direction = ParameterDirection.Input;
cn.Open();
MySqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
txtName.Text = patient.Name = (String)reader[0];
txtSurname.Text = patient.Surname = (String)reader[1];
txtFathersname.Text = patient.Fathersname = (String)reader[2];
txtStatus.Text = patient.Status = (String)reader[3];
txtSuccessDescription.Text = patient.SuccessDescription = (String)reader[4];
patient.Id = (int)reader[5];
}
reader.Close();
cn.Close();
}
}
protected void Update_Click(object sender, EventArgs e)
{
String cs = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
using (MySqlConnection cn = new MySqlConnection(cs))
{
MySqlCommand command = new MySqlCommand(StoredProcedures.update_patient, cn);
command.CommandType = CommandType.StoredProcedure;
patient.Name = txtName.Text.Trim();
patient.Surname = txtSurname.Text.Trim();
patient.Fathersname = txtFathersname.Text.Trim();
patient.Status = txtStatus.Text.Trim();
patient.SuccessDescription = txtSuccessDescription.Text;
patient.Id = int.Parse(Request.QueryString["patientId"]);
command.Parameters.Add("pname", MySqlDbType.VarChar).Value = patient.Name;
command.Parameters.Add("psurname", MySqlDbType.VarChar).Value = patient.Surname;
command.Parameters.Add("pfathersname", MySqlDbType.VarChar).Value = patient.Fathersname;
command.Parameters.Add("pstatus", MySqlDbType.VarChar).Value = patient.Status;
command.Parameters.Add("psuccessdescription", MySqlDbType.Text).Value = patient.SuccessDescription;
command.Parameters.AddWithValue("pid", patient.Id);
command.Parameters["pid"].Direction = ParameterDirection.Input;
cn.Open();
if (command.ExecuteNonQuery() > 0)
{
ResultLabel.Text = "";
ResultLabel.Text = "Update success";
}
cn.Close();
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
return;
// Rest of your code here
}
在回發的情況下,您不想覆蓋用戶鍵入的內容。
以下是有關ASP.NET頁面生命周期的一些文檔。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.