tags:

views:

321

answers:

3

I have this solution for a single button:

myButton.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(myButton).ToString());

Which works pretty well for one button, any ideas on how to expand this to 2 buttons?

+2  A: 

You could add an clientside onSubmit handler, or you could do this:

myButton.Attributes.Add("onclick", "this.disabled=true; document.getElementById('" 
+ button2.ClientID + "').disabled = true;" 
+ GetPostBackEventReference(myButton).ToString());
FlySwat
This answer is probably better syntax wise.
Ian Jacobs
Whats the quoting on this single quotes around double quotes? For the getElementById
Brian G
GetElementById takes a string, hence the single quotes.
FlySwat
A: 

change the command to:

myButton.Attributes.Add("onclick", "this.disabled=true;document.getElementbyID("Button2").disabled=true;" + GetPostBackEventReference(myButton).ToString());
Ian Jacobs
You should use ClientID in case the button is inside a naming container.
EndangeredMassa
+1  A: 

Change the javascript to reference the other button as well.

var btn1 = document.GetElementById('btn1ID');
var btn2 = this;

btn1.disabled = true;
btn2.disabled = true;

If the buttons are in a naming container, you'll need to use the .NET object's property called ClientID to get the html ID of the element.

var btn1 = document.GetElementById('<%= btn1.ClientID %>');

I suggest wrapping these in a script tag and a function, then just call the function from your .NET attribute addition.

CodeBehind

btn2.Attributes.Add("onclick", "handleClick();")

ASPX

<script type="text/javascript">
    function handleClick() {
        var btn1 = document.GetElementById('<%= btn1.ClientID %>');
        var btn2 = this;

        btn1.disabled = true;
        btn2.disabled = true;

    }
</script>

<asp:Button id="btn1" runat="server" text="Button 1" />
<asp:Button id="btn2" runat="server" text="Button 2" />
EndangeredMassa
It would be simpler to use Jonathan Holland's solution.
EndangeredMassa