The events actually take place after the render of the control. This change would only be picked up on the second page reload so the render knows to add this attribute.
Instead make the change at the client side using javascript or try to intercept the event on page load or init.
Making the change on the client side would not persist after a page reload unless you are performing a partial postback:
How to enable partial rendering with the AJAX UpdatePanel[
^]. The button would have to be an external trigger otherwise it would get re-rendered.
Intercepting the button click on page load or init isn't too tricky. You need to get the
Request.Form["__EVENTTARGET"];
which is the unique id of the control that triggered the postback. I use this in itembound repeaters because I prefer to have all of my backend code in c#. I hardly ever use asp.net in the markup.
Your code might look like this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
var requestTarget = Request.Form["__EVENTTARGET"];
var btnUniqueId = Button1.UniqueID;
if(requestTarget == btnUniqueId)
Button1.Attributes.Add("style", "background-color: #0066FF");
}
This will set the button colour once. There is no way included in this example to reset it, but it can be done simply enough.
Hope that helps ^_^
Andy