i need to be able to disable a button

Gambit_NI

Active member
Joined
Jun 16, 2005
Messages
42
Location
Belfast
Programming Experience
3-5
i have a serverside button on my webpage which the user clicks to save

the save can take a bit of time so basically i need some way of disabling the button once they click save. otherwise the user tends to hit save again then i get a concurrency error!

any idea's/code welcome!!

Cheers,
Craig
 
Can you just use the visible property of the button to hide it while the save is in progress ?

Then set the property back to 'True' once the save has been completed.

VB.NET:
<asp:Button
ID="btnSave"
Text="Save"
runat="server" />
 
Sub Save
 
'Hide the button 
btnSave.Visible=False
 
'save routine goes here
 
'Show the button 
btnSave.Visible=True
 
End Sub
 
Well, you can use visible property of certain button but if you don't want to hide a button and just make it unfunctional after one click then you should consider addhandler and removehandler statements ... maybe even you can use boolean variable that would be checked before action continue ..i.e. if myBool = true then Exit Sub nad for the first time it should look like ... if myBool = false then myBool = true {...rest of the code...}

Cheers ;)
 
my problem was that disabling it, or making it invisible wasnt possible until the page posted back. i needed something to disable it as soon as the user clicked it.

in the end i got this snippet of code which does exactly what i need, and made it into a little reusable function

VB.NET:
[size=2][/size][size=2][color=#0000ff]Private[/color][/size][size=2] [/size][size=2][color=#0000ff]Function[/color][/size][size=2] fun_AttributeDisableSaveButton([/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] ctlSaveButton [/size][size=2][color=#0000ff]As[/color][/size][size=2] Control, [/size][size=2][color=#0000ff]ByVal[/color][/size][size=2] strCancelButtonName [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String[/color][/size][size=2]) [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]String

[/color][/size][size=2][/size][size=2][color=#008000]'ctlSave must have its Causes Validation property set to false

[/color][/size][size=2][/size][size=2][color=#0000ff]Dim[/color][/size][size=2] sb [/size][size=2][color=#0000ff]As[/color][/size][size=2] [/size][size=2][color=#0000ff]New[/color][/size][size=2] System.Text.StringBuilder

sb.Append("if (typeof(Page_ClientValidate) == 'function') { ")

sb.Append("if (Page_ClientValidate() == false) { return false; }} ")

sb.Append("this.value = 'Please wait...';")

sb.Append("this.disabled = true;")

sb.Append("document.getElementById('")

sb.Append(strCancelButtonName)

sb.Append("').disabled = true;")

sb.Append(Page.GetPostBackEventReference(ctlSaveButton))

sb.Append(";")

[/size][size=2][color=#0000ff]Return[/color][/size][size=2] sb.ToString

[/size][size=2][color=#0000ff]End[/color][/size][size=2] [/size][size=2][color=#0000ff]Function

[/color][/size]

then in the Page Load just do the following
VB.NET:
[size=2]btnSave2.Attributes.Add("onclick", fun_AttributeDisableSaveButton(btnSave2, "btnCancel2"))
[/size]
 
Back
Top