0

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();
}
wolfsshield
  • 757
  • 5
  • 14
  • I haven't worked with WebForms in years (and I advise you to switch to MVC). Regardless, a button click event will fire a postback. If you don't have a `if (!IsPostBack) { // stuff here }`, then your `Page_Load` event will fire every button click (minus ajax, jquery, etc). Perhaps instead of calling all those methods for `get.....`, pass querystrings so you can obtain the needed values easier – Rob Scott Mar 08 '19 at 01:12
  • Investigating and running through the tutorial at https://www.tutorialspoint.com/mvc_framework/ While I now have a functional website, learning this MVC methodology and toolset may allow for better site maintenance. – wolfsshield Mar 08 '19 at 18:51

1 Answers1

0

I've taken the complete code and stripped it down to just the buttons, and when I paste that into a new webpage, that page executes correctly.

By commenting out the very things I stripped out in my small sample, and then slowy adding one element at a time, I discovered that the culprit is the code Server.Transfer("page.aspx");

Every button that has this code requires the button to be selected twice for the button to fire.

Since the first page is using this to navigate to the second page, any asp control button has to be pushed before any of the buttons will execute correctly.

So I am removing the use of Server.Transfer("page.aspx"); and looking for alternatives.

I have instead used code like prev_BTN.Attributes.Add("href", "RecordDetailPage?RecNum=" + getPreviousRecord()); and RORQL_BTN.Attributes.Add("href", "RecordLstPage?ViewEdit=view"); inconjunction with String REC = Request.QueryString["RecNum"]; and String REC = Request.QueryString["ViewEdit"]; within the page load screens.

Now I have the data I want passing from screen to screen and I don't have the weirdness of having to click buttons multiple times.

wolfsshield
  • 757
  • 5
  • 14