Snooley
Snooley

Reputation: 71

C# hide and show buttons on click

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;
        }
    }
}

Upvotes: 0

Views: 249

Answers (1)

Longoon12000
Longoon12000

Reputation: 784

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.

Upvotes: 1

Related Questions