0

I am trying to display run button which executes a stored procedure, that on click hides and another button appears showing a 'spinner/loader' button until the execution is complete returning back to run.

When I click run, the procedure executes although I don't see any change with the buttons. When I remove the code at the bottom, the buttons change after the stored procedure has finished.

At the top of my click event I have:

btnRun.Visible = false; LoadButton.Visible = true; 

and at the bottom:

LoadButton.Visible = false; btnRun.Visible = true; 

I am guessing this is to do with post back, is there away around this?

Edit: This is my click event -

protected void Run_Click(object sender, EventArgs e) { using (SqlConnection con = new SqlConnection(dbString)) using (SqlCommand cmd = new SqlCommand("dbo.ExecuteJob", con)) { try { btnRun.Visible = false; LoadButton.Visible = true; System.Threading.Thread.Sleep(100); string JobName = "CTIOM: " + ddlJob.SelectedItem.Text; cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@job_name", JobName); SqlParameter statusParameter = new SqlParameter { ParameterName = "@returnStatus", SqlDbType = SqlDbType.Int, //Size = 255, Direction = ParameterDirection.Output }; SqlParameter messageParameter = new SqlParameter { ParameterName = "@returnMessage", SqlDbType = SqlDbType.VarChar, Size = 1000, Direction = ParameterDirection.Output }; SqlParameter durationParameter = new SqlParameter { ParameterName = "@returnRunDuration", SqlDbType = SqlDbType.VarChar, Size = 8, Direction = ParameterDirection.Output }; cmd.Parameters.Add(statusParameter); cmd.Parameters.Add(messageParameter); cmd.Parameters.Add(durationParameter); con.Open(); IAsyncResult result = cmd.BeginExecuteNonQuery(); while (!result.IsCompleted) { System.Threading.Thread.Sleep(100); } cmd.EndExecuteNonQuery(result); string statusMessage = statusParameter.Value.ToString(); lblStatus.Visible = true; lblStatus.Text = statusMessage; string returnMessage = messageParameter.Value.ToString(); lblMessage.Visible = true; lblMessage.Text = returnMessage; } catch (SqlException ex) { ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('" + ex.Message.ToString() + "')", true); return; } finally { con.Close(); LoadButton.Visible = false; btnRun.Visible = true; } } } 
4
  • if you change the UI you need to call invalidate so it will repaint. Commented Oct 25, 2019 at 13:54
  • What do you have between? If its in the same thread the elements will not refresh. Commented Oct 25, 2019 at 13:54
  • Show the code related to the btnRun click event... Commented Oct 25, 2019 at 13:56
  • @turanszkik they're in a try and finally. There is a loop in the try too so the procedure has time to finish. Commented Oct 25, 2019 at 13:59

1 Answer 1

1

Changes to elements are only sent once after the postback, not immediately when you change them. Pretty sure to accomplish what you are trying to do (display a spinner while the postback is running) you will need to use something like the UpdateProgress control.

Sign up to request clarification or add additional context in comments.

1 Comment

Had a mess with that code and managed to get it working as expected, thanks!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.