我有一个带有 autopostback 属性 true 的文本框和一个保存按钮。
在文本更改时,我从数据库中获取数据,并显示在其他文本框中。当我编辑文本框然后保存时,会触发 textchanged 事件,并且数据不会更新。相反,它只保存以前的数据。我想保存编辑过的数据。
谁能帮我做到这一点?
这是按钮单击事件:
protected void btnSave_Click(object sender, EventArgs e)
{
//TextBox1.Text = exists;
if(exists=="yes")
{
OdbcConnection DbConnection2 = new OdbcConnection(con1);
OdbcCommand DbCommand2 = DbConnection2.CreateCommand();
try
{
DbConnection2.Open();
DbCommand2.CommandText = "update tbl_trip_login set username='" + txtUser.Text.ToUpper() + "',password='" + txtPwd.Text + "',name='" + txtname.Text + "',department='" + txtDept.Text + "',designation='" + txtDesg.Text + "',DOJ='" + txtDOJ.Text + "',DOB='" + txtDOJ.Text + "',phone='" + txtPh.Text + "',Email='" + txtEmail.Text + "',Gender='" + rdbGender.SelectedItem.Text + "',Active='" + rdbActive.SelectedItem.Text + "' where upper(username)=upper('" + txtUser.Text + "')";
TextBox1.Text = DbCommand2.CommandText.ToString();
int t2 = DbCommand2.ExecuteNonQuery();
if (t2 == 1)
{
lblError.Visible = true;
lblError.Text = "Successfully Saved";
}
else
{
lblError.Text = "Not Saved.. Try again later";
lblError.Visible = true;
}
DbConnection2.Close();
}
catch (Exception e1)
{
lblError.Text = e1.Message;
lblError.Visible = true;
DbConnection2.Close();
}
}
else //if user doesn't exists,create new user
{
DateTime dd = DateTime.Now; //current date and time
string custom = dd.ToString("dd-MMM-yyyy,hh:mm,tt"); // to convert in the form : 14-APR-2013 01:35 PM
string ss = "to_date('" + custom + "','dd-mon-yyyy HH:MI AM')"; // oracle does not take only time by default, so use to_date(), and pass this string variable directly in the query
OdbcConnection DbConnection1 = new OdbcConnection(con1);
OdbcCommand DbCommand1 = DbConnection1.CreateCommand();
try
{
DbConnection1.Open();
DbCommand1.CommandText = "insert into tbl_trip_login(username,password,name,department,designation,DOJ,DOB,phone,Email,Gender,Active,Entered_by,Entered_time)values('" + txtUser.Text + "','" + txtPwd.Text + "','" + txtname.Text + "','" + txtDept.Text + "','" + txtDesg.Text + "','" + txtDOJ.Text + "','" + txtDOB.Text + "','" + txtPh.Text + "','" + txtEmail.Text + "','" + rdbGender.SelectedItem.Text + "','" + rdbActive.SelectedItem.Text + "','" + Session["UserAuthentication"].ToString() + "','" + ss + "')";
TextBox1.Text = DbCommand1.CommandText.ToString();
int t1 = DbCommand1.ExecuteNonQuery();
if (t1 == 1)
{
lblError.Visible = true;
lblError.Text = "Successfully Saved";
}
else
{
lblError.Text = "Not Saved.. Try again later";
lblError.Visible = true;
}
DbConnection1.Close();
}
catch (Exception e1)
{
lblError.Text = e1.Message;
lblError.Visible = true;
DbConnection1.Close();
}
txtUser.Focus();
txtUser.Text = "";
txtPwd.Text = "green";
}
}
和 Text Changed 事件:
protected void txtUser_TextChanged(object sender, EventArgs e)
{
var targetID = Request.Form["__EVENTTARGET"];
if (targetID != null && targetID != string.Empty)
{
btnSave.Enabled = true;
exists = "no";
try
{
OdbcConnection myOdbcConnection = new OdbcConnection(con1);
OdbcCommand myOdbcCommand = myOdbcConnection.CreateCommand();
string sSQL = "select username,password,name,department,designation,doj,dob,phone,email,gender,active from tbl_trip_login where upper(username)=upper('" + txtUser.Text + "')";
myOdbcCommand.CommandText = sSQL;
myOdbcConnection.Open();
OdbcDataReader myOdbcDataReader = myOdbcCommand.ExecuteReader();
if (myOdbcDataReader.HasRows)
{
exists = "yes";
txtPwd.Text = myOdbcDataReader[1].ToString();
txtname.Text = myOdbcDataReader[2].ToString();
txtDept.Text = myOdbcDataReader[3].ToString();
txtDesg.Text = myOdbcDataReader[4].ToString();
txtDOJ.Text = myOdbcDataReader[5].ToString();
txtDOB.Text = myOdbcDataReader[6].ToString();
txtPh.Text = myOdbcDataReader[7].ToString();
txtEmail.Text = myOdbcDataReader[8].ToString();
rdbGender.Text = myOdbcDataReader[9].ToString();
rdbActive.Text = myOdbcDataReader[10].ToString();
}
}
catch (Exception ex)
{
lblError.Text = ex.Message;
lblError.Visible = true;
}
}
}