I only want the Page_Load
method to fire after the save method has been completed.
I have created a web page/site that serves as the user interface to a MySQL database. Originally I used purely JavaScript and HTML, and it was functional, but annoying because users would have to hit three acknowledgment buttons any time they navigated from one page to another.
I have eliminated that annoyance by shifting the database calls to the .aspx.cs files, as well as some of the population of the forms and reports. However, while the page/site is now functional again, now it requires a user to press the save button then insert the data they wish to save to the database, and press save again. Likewise, if they wish to see a record in detail view, and then cycle through the records, they have to hit the next or previous buttons twice.
I have traced through on the debug and the first time the buttons are clicked, it hits the Page_Load
method, but nothing else. Then, when you click a second time it hits the method the button is linked to, and then hits the Page_Load
method.
This is very annoying because a user would expect to put in the information once, and hit save and have the information stay put...not vanish requiring a second entry.
I don't want to turn off the pageload as seems to be done here: https://stackoverflow.com/a/19454346/11035837
I only want it to fire after the save method has been completed.
How do I do this?
Following are the c# methods I'm using (stripped down). As best as I can tell the order of their placement in the file has no effect.
Why would the Page_Load
method be called at all when the saveRecord
method is the referenced method in the button construction?
protected void Page_Load(object sender, EventArgs e)
{
loadRecord();
loadBackButton();
loadRecordCycleButtons();
loadSaveButton();
}
protected void loadSaveButton()
{
SAVE_PH.Controls.Clear();
string prevPage = getPreviousPage();
string accessStatus = getViewEdit();
LinkButton save_BTN = new LinkButton();
save_BTN.ID = "save_BTN";
save_BTN.Text = "Save"; save_BTN.Visible = true;
save_BTN.BackColor = Color.Green; save_BTN.ForeColor = Color.Black; save_BTN.BorderColor = Color.Gray;
save_BTN.BorderStyle = BorderStyle.Solid; save_BTN.BorderWidth = 2; save_BTN.Width = 60;
save_BTN.Attributes.Add("style", "text-align:center; border-color: white gray gray white; text-decoration:none");
save_BTN.Click += new EventHandler(saveRecord);
if (accessStatus != "View") { SAVE_PH.Controls.Add(save_BTN); }
}
protected void saveRecord(object sender, EventArgs e)
{ string RecordNumber =getCurrentlySelectedRecord();
PlaceHolder MAIN = Page.FindControl("RECORD_PH") as PlaceHolder;
/*...fetch values for parameters ...*/
MySqlConnection conn = new MySqlConnection();
conn.ConnectionString = getConnectionString();
conn.Open();
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "UPDATE_RECORD";
cmd.Parameters.Clear();
cmd.Parameters.Add(new MySqlParameter("@V_RECORD_NUMBER", RecordNumber)); cmd.Parameters["@V_RECORD_NUMBER"].Direction = ParameterDirection.Input;
/*...Set other parameters...*/
try
{
cmd.ExecuteNonQuery();
}
catch (MySql.Data.MySqlClient.MySqlException ex)
{
Response.Write("<script>alert(\"Error\"+" + ex.ToString() + " );</script>");
var abc = "abc";//debug line for tracing
}
conn.Close();
}