disable button on click

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>
 
I

Infant Newbie

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
 
S

S. Justin Gengo

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
 
N

nicholas

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>
 
N

nicholas

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>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,898
Latest member
BlairH7607

Latest Threads

Top