Instantly disable a server side asp.net button?

D

Dot net work

Hello.

I've read many posts about disabling submit buttons, but I can't get
these answers to solve my problem.

I have a server side asp.net button, and under the button I have code
behind that does some simple processing, followed by a
response.redirect.

When the user clicks on this button, I would like the button to
instantly appear to be "ghosted / grayed out / disabled", like a
typical disabled button, then carry on and do the code behind
processing, followed by the response.redirect

One of the things I tried doing was putting in client side javascript
to disable the button, but that had the effect of stopping all code
running inside the server button click event handler.

Can anyone solve this dilemna please?

TIA, dnw.
 
J

John Timney \(ASP.NET MVP\)

Why dont you put it in a DIV and hide the div when the button is clicked
 
D

Dot Net Work

Hello.

The result is very interesting!

If the asp button has no server side event click handler, then your
suggestion works great!

If the asp button has a server side event click handler, even if there
is absolutely no code inside this handler, your suggestion does not hide
the div (and subsequently does not hide the button.)

Unforunately, I need code behind processing inside the asp button click
handler!

-dnw.
 
D

Dot Net Work

I have managed to find a solution, although it is not a clean and
elegant one.

Have an asp.net button and an html button

Inside the code behind page init handler:

aspnetbutton.Attributes.Add("onClick", "javascript: func();")

Inside the HTML section:

For the html button, set it's style property:

VISIBILITY: hidden; disabled type="button"

For the javascript function called func():

function func()
{
document.getElementById('aspnetbutton').style.visibility='hidden';
document.getElementById('html button').style.visibility='visible';
}

Basically, when the user clicks on the asp.net button, the client side
function gets called, and it hides the aspnetbutton, and makes the html
disabled button visible.

The trick is to position the hidden html button in exactly the same
place as the visible asp.net button, so that they overlap at design
time.

Actually, I'm not sure why I had difficulty in hiding the DIV now, if I
can successfully hide an asp.net button.

-dnw.
 
R

Rick Spiewak

Here's another approach:

1. Use the following javascript following your form (changing the names as
needed):

</form>

<%if disableMe then%>
<script language="javascript">
var disabled = true;
document.all.btnDisable.disabled = true;
</script>
<%else%>
<script language="javascript">
var disabled = false;
document.all.btnDisable.disabled = false;
</script>
<%end if%>

<script language="javascript">
function shouldsubmit()
{
if (disabled==true)
{
return false;
}
else
{
disabled = true;
return true;
}
}

</script>
</body>

2. Add the following code (or similar) to the code-behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Session("disableMe") Is Nothing Then
disableMe = Session("disableMe")
End If
End Sub

Protected disableMe As Boolean = False
Private Sub btnDisable_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDisable.Click
disableMe = True
Session("disableMe") = disableMe
End Sub
 
D

Dot net work

I like it! :eek:)

Rick Spiewak said:
Here's another approach:

1. Use the following javascript following your form (changing the names as
needed):

</form>

<%if disableMe then%>
<script language="javascript">
var disabled = true;
document.all.btnDisable.disabled = true;
</script>
<%else%>
<script language="javascript">
var disabled = false;
document.all.btnDisable.disabled = false;
</script>
<%end if%>

<script language="javascript">
function shouldsubmit()
{
if (disabled==true)
{
return false;
}
else
{
disabled = true;
return true;
}
}

</script>
</body>

2. Add the following code (or similar) to the code-behind:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not Session("disableMe") Is Nothing Then
disableMe = Session("disableMe")
End If
End Sub

Protected disableMe As Boolean = False
Private Sub btnDisable_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnDisable.Click
disableMe = True
Session("disableMe") = disableMe
End Sub
 
D

Dot net work

Hi,

I've found out something interesting.

If you try this inside a web user control, the <% ... %> scripting
code does not get called when you click a button. It does get called
when the form is first loaded, but never afterwards.

Also, you have shouldsubmit() javascript function in your example
code, but it does not get used in your example code below. Please can
you tell me where this gets called. Thanks a lot.

-dnw.
 
D

Dot net work

Hi,

Actually, after testing this carefully, I've realised that it doesn't
address what I was hoping to achieve: when the user clicks the button,
I need the button to *instantly* become disabled, just like websites
such as paypal do.

When I tested the code below, I realised that the button became
disabled after posting back to the server. I was hoping for a much
quicker reaction.

Perhaps my awful kludge with the 2 button approach mentioned earlier
is the only way? I hope not, but I never found any solution to this
looking through many many posts about the subject.

-dnw.
 
R

Rick Spiewak

It may need:
document.all.btnDisable.disabled = true;
added to the shouldsubmit code after the else

Or maybe not. For some reason, this leaves the button appearing
enabled...
 
R

Rick Spiewak

It may need:
document.all.btnDisable.disabled = true;
added to the shouldsubmit code after the else
 
D

Dot net work

This code has the very interesting side effect of nullifying the
server side click event handler. So, once the button is disabled in
this manner (on the client side), no server side click code runs.

Frustrating, but interesting nonetheless.

-dnw.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top