disable button on click

  • Thread starter Thread starter nicholas
  • Start date Start date
N

nicholas

how can I disable a button when the user clicks on it and the webform is
validated.

I want this to prevent the user from clicking twice and by this submitting
the form twice.

Thank you.

This is what I tried, but it does nothing:

<script runat="server">
sub submit_it (Obj as Object, e as EventArgs)
if (Page.IsValid) then
BtnSubmit.text = Not(BtnSubmit.Enabled)
end if
End sub
</script>
 
set a page-level variable , boolean boolAllow
then in ok click event

if boolAllow = true then
return
bAllow = true
btnOK.enable = false
DO YOUR STUFF HERE
btnok.enable = true ---- if u want
boolAllow = false

MIND - NOT REAL CODE ABOVE

bart
http://www.meshcode.net
 
Nicholas,

This javascript will do the trick for you. It only disables the button if
the form passes validation:

<script language="javascript">
<!--
var submitcount=0;

function disableSubmit()
{
if (typeof(Page_ClientValidate)=='function')
{
if (Page_ClientValidate() == true)
{
return checkSubmit();
}
else
{
return true;
}
}
else
{
return checkSubmit();
}
}

function checkSubmit()
{
if (submitcount == 0)
{
submitcount++; return true;
}
else
{
alert('This form has already been submitted.'); return false;
}
}
//-->
</script>

In VB.Net Attach it to your button like so:

SubmitButton.Attributes.Add("onClick", "javascript: return
disableSubmit();")


If you'd like I've encapsulated this script and a method for attaching it to
any button in a component I offer for free on my web site,
www.aboutfortunate.com.

Just go to the code library and then click the Javascript button in the menu
on the left. The entire component's source code is available (as a .Net
project) and it contains some other useful javascripts.


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

Free code library at:
www.aboutfortunate.com

"Out of chaos comes order."
Nietzche
 
Although I don't realy understand what you are trying to do, this is what I
tried without succes:

<script runat="server">
dim boolAllow as boolean
sub submit_it (Obj as Object, e as EventArgs)
if boolAllow = true then
return
boolAllow = true
BtnSubmit.enabled = false
boolAllow = false
if (Page.IsValid) then
BtnSubmit.enabled = False
end if
end if
End sub
</script>
 
That's what I tried first, but that does not work (although it doesn't
generate an error):

<script runat="server">
sub submit_it (Obj as Object, e as EventArgs)
BtnSubmit.enabled = false
End sub
</script>
 
Back
Top