Disable submit button after user click

G

GS

Hi,

This is probably known solution but I have not found it in minimum amount of code.
I'd like to disable button on client-side after clicking on it.
Just disabling it in javascript do not produce postback event.
Any examples in ASP.NET 2.0?

Thanks,
 
P

Peter Zolja

This is probably known solution but I have not found it in minimum amount
of code.
I'd like to disable button on client-side after clicking on it.

Don't know about 2.0, but this works in 1.1, give it a try:

1. in the PageLoad event

if (!IsPostBack)
{
ibSomeButton.Attributes.Add("onclick",
"this.style.display='none';");
// other stuff
}

The "ibSomeButton" is an ImageButton. However, this will not prevent the
user from hitting refresh which can generate a postback.
 
T

tdavisjr

Peter said:
Don't know about 2.0, but this works in 1.1, give it a try:

1. in the PageLoad event

if (!IsPostBack)
{
ibSomeButton.Attributes.Add("onclick",
"this.style.display='none';");
// other stuff
}

The "ibSomeButton" is an ImageButton. However, this will not prevent the
user from hitting refresh which can generate a postback.

This will hide the button and not disable it. But the goad is still
acheived. May this will work:

idSomeButton.Attributes.Add("onclick", "this.disabled= 'disabled' ");
 
P

Peter Zolja

This will hide the button and not disable it. But the goad is still
acheived. May this will work:

idSomeButton.Attributes.Add("onclick", "this.disabled= 'disabled' ");

That's correct, but form my experience users don't notice the disabled
state... and still try to click it... so what I do is hide the button and
unhide some text that tells them to wait...
 
G

GS

Thanks,

But I wanted to use Button not ImageButton.
I actuallty found the piece of code to do it, the only problem is that
Page.GetPostBackEventReference is obsolete and I'm supposed to use some new
ASP.NET 2.0 method which I have not figured out how.

btnGetImportInfo.Attributes["onclick"] =
"this.disabled=true;this.value='Please wait...';" +
Page.GetPostBackEventReference(btnGetImportInfo ).ToString();
 
D

drflower

This will hide the button and not disable it. But the goad is still
acheived. May this will work:

idSomeButton.Attributes.Add("onclick", "this.disabled= 'disabled' ");

I have wanted to do this for a long time, however using a method such
as this one does not work (or at least, did not work for me in v1.1)
because if the page was invalid, the onclick would disable the button,
but the form would not be submitted due to the invalid data.

For example, imagine there is a required field called name, which the
user leaves blank and presses submit. The submit button gets disabled,
and then the user is asked to enter their name. Problem.

Or am I missing something?

Daniel.
 
P

Peter Zolja

Or am I missing something?

If you do the field checking on the client side then yes, you have a
problem -- which can be solved by checking first and only if successful
disable/hide the button. If, however, you allow a postback and do the
checking on the server side everything should be fine since after a postback
the page comes back with the button enabled/visible -- the default value...
 
P

Peter Zolja

But I wanted to use Button not ImageButton.

I image it's the same... haven't tried though... (my code was using an
ImageButton and I just took it from there)
I actuallty found the piece of code to do it, the only problem is that
Page.GetPostBackEventReference is obsolete and I'm supposed to use some
new ASP.NET 2.0 method which I have not figured out how.

btnGetImportInfo.Attributes["onclick"] =
"this.disabled=true;this.value='Please wait...';" +
Page.GetPostBackEventReference(btnGetImportInfo ).ToString();

Sorry, I'm still in the 1.1 land. Untested code, try this:

btnGetImportInfo.Attributes.Add("onclick",
"this.disabled=true;this.value='Please wait...';");
 
G

GS

Code below will not work since disabling button happens before postback and
hence no postback happens when you disable button.

Peter Zolja said:
But I wanted to use Button not ImageButton.

I image it's the same... haven't tried though... (my code was using an
ImageButton and I just took it from there)
I actuallty found the piece of code to do it, the only problem is that
Page.GetPostBackEventReference is obsolete and I'm supposed to use some
new ASP.NET 2.0 method which I have not figured out how.

btnGetImportInfo.Attributes["onclick"] =
"this.disabled=true;this.value='Please wait...';" +
Page.GetPostBackEventReference(btnGetImportInfo ).ToString();

Sorry, I'm still in the 1.1 land. Untested code, try this:

btnGetImportInfo.Attributes.Add("onclick",
"this.disabled=true;this.value='Please wait...';");
 
Joined
May 4, 2007
Messages
1
Reaction score
0
Disable the Button, Then Manually Submit the Form

Try this:

<input type="submit" id="Submit" name="Submit" value="Submit" onclick="javascript:document.form1.Submit.disabled=1; document.form1.submit();">
 
Joined
Aug 16, 2009
Messages
1
Reaction score
0
check this out too -
ht tp://blog.ysatech.com/post/2009/08/13/Disable-an-ASPNET-button-during-PostBack-with-AJAX-loading-background-image.aspx
 
Joined
May 14, 2011
Messages
2
Reaction score
0

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,755
Messages
2,569,537
Members
45,023
Latest member
websitedesig25

Latest Threads

Top