I fill textboxes with data that i get from database and put a button on page that user can edit data and click on this button then from code behind i set changes to database
but when i want to send data (for example txtName.Text that source value is “John” and i change it to “Tom”) to database i see txtName.Text is “John” (means last value no new value)
why?
//Load Data
protected void Page_Load(object sender, EventArgs e)
{
DataSet ds = ....
txtName.Text = ds.Tables[0].Rows[0][1].ToString();
}
//Update
protected void reg_Click(object sender, EventArgs e)
{
string name=txtName.Text;
//i change value of txtName.Text but see value as same as value in ds.Tables[0].Rows[0][1].ToString() that i select from DB
}
This is because you are overwriting the value of the textbox during each request. If you put the code into a
if (!IsPostBack)then it will work:If you place a breakpoint in both
Page_Loadandreg_Click, then things will become obvious.Page_Loadis called on every request. In case of the postback (caused by clicking the button), it is called before the button click handler, and will therefore overwrite the (edited) value of the textbox with the original value read from the database.