I have a button called Button1, and I have the following code-behind:

string CS = ConfigurationManager.ConnectionStrings["EasyRozMoney_ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
    con.Open();
    SqlCommand cmd = new SqlCommand("spTaskPerformed", con);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@TaskId", lblTaskId.Text);
    cmd.Parameters.AddWithValue("@Email", Session["Email"].ToString());
    cmd.ExecuteNonQuery();
    Button1.BackColor = Color.Red;
    Button1.Enabled = false;
    lblTaskPerformed.Text = "Task Completed Successfully.";
}

Everything else works fine.The button gets disabled too but it doesn't change its background color. Any suggestions?

Update:

The inline style on your button is preventing the background color from showing up.

Here's why:

Consider the following ASPX code:

<!-- language: lang-html --> <pre><code>&lt;asp:Button runat=&quot;server&quot; ID=&quot;Button1&quot; Text=&quot;Click Me!&quot; <b>style=&quot;background-color: yellow;&quot;</b>/&gt; </code></pre>

This will render the following HTML:

<!-- language: lang-html --> <pre><code>&lt;input type=&quot;submit&quot; id=&quot;MainContent_Button1&quot; value=&quot;Click Me!&quot; <b>style=&quot;background-color: yellow;&quot;</b>&gt; </code></pre>

When we add a BackColor in the code behind, it prepends an inline style into the element's style attribute. Which produces this HTML:

<!-- language: lang-html --> <pre><code>&lt;input type=&quot;submit&quot; id=&quot;MainContent_Button1&quot; value=&quot;Click Me!&quot; <b>style=&quot;background-color:Red;background-color: yellow;&quot;&gt;</b> </code></pre>

So it inserts the red color, but then the original color immediately overrides it.

To solve this, in your ASPX, use the BackColor property instead of using the background-color inline style tag. Like this:

<!-- language: lang-html --> <pre><code>&lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; Text=&quot;Submit&quot; <b>BackColor=&quot;#CC6600&quot;</b> <strike>style=&quot;background-color:#CC6600;&quot;</strike>/&gt; </code></pre>

ASP.NET will know how to properly override this when new colors are applied.

Original:

If you had to do this in JavaScript, You can use RegisterStartupScript to send dynamic javascript code to the browser, but I suspect something else is at issue.

<!-- language: lang-cs -->
var script = "document.getElementById('" + Button1.ClientID + "').style.backgroundColor = 'red';";
ScriptManager.RegisterStartupScript(this, typeof(Page), "changecolor", script, true);