473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Instantly disable a server side asp.net button?

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.
Nov 18 '05 #1
11 6924
Why dont you put it in a DIV and hide the div when the button is clicked

--
Regards

John

"Dot net work" <do***@hotmail.com> wrote in message
news:77**************************@posting.google.c om...
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.

Nov 18 '05 #2


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.
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #3

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.visi bility='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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #4
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

"Dot Net Work" <do***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

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.visi bility='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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #5
I like it! :o)

"Rick Spiewak" <ri*********@mindspring.com> wrote in message news:<en**************@TK2MSFTNGP12.phx.gbl>...
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

"Dot Net Work" <do***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

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.visi bility='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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #6
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.
"Rick Spiewak" <ri*********@mindspring.com> wrote in message news:<en**************@TK2MSFTNGP12.phx.gbl>...
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

"Dot Net Work" <do***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

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.visi bility='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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #7
The <form> tag uses onsubmit="return shouldsubmit()"

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #8
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.
"Rick Spiewak" <ri*********@mindspring.com> wrote in message news:<en**************@TK2MSFTNGP12.phx.gbl>...
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

"Dot Net Work" <do***@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...

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.visi bility='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.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #9

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...
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #10

It may need:
document.all.btnDisable.disabled = true;
added to the shouldsubmit code after the else
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 18 '05 #11
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.

Rick Spiewak <ri*********@mindspring.com> wrote in message news:<#Y*************@TK2MSFTNGP10.phx.gbl>...
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...
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 18 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jay | last post by:
I am trying to get the caption (value) of the server side button to use it in a query. The function below is a database function which needs the Field to search in and the Value to search for. ...
2
by: cw | last post by:
Hi group, My page consist of few text boxes and a server side button control (asp:button), when i finished editing and press enter key, it will fire up the button click event. How to disable this...
1
by: C | last post by:
Hi, I have a server side asp.net button on a web page. On clicking the button I want to call some Javascript. Can I not simply do a onclick="MyJavascript();" or is it necessary that I add...
1
by: Bin Song, MCP | last post by:
Hi all I got a strange problem In my webform, I have a server-side Button "btnDisable" to save some data and enable or disable all other fields in this form based on the data. The logic is: on...
4
by: Ryan Ternier | last post by:
I have a button event: Public Sub SwitchItem(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim btnTest As New Button Dim astrTest As String() btnTest = CType(sender, Button)...
2
by: alan | last post by:
Hi all, I need to pass a variable to the client-side vbscript and when the button is pressed invoke the script (SomeSub()). The problem is, that the "blablabla" appears only after second...
6
by: shil | last post by:
I have a server side button that needs to do some preperation and afterwards, if everything goes well, open a new browser with a specific url. I know how to execute javascript from a server side...
3
by: Steve Kershaw | last post by:
Hi, I need to fire a server side button click event from my client side javascript. The client side javascript code follows: <script language="javascript" type="text/javascript"> function...
1
by: =?Utf-8?B?Vk1J?= | last post by:
I have several RequiredFieldValidator in my page, but I need to do postback, so I have to disable them temporarily. I do this with Page_ValidationActive on client-side...
1
by: babashankar | last post by:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <script type="text/javascript"> var change = false; function showmsg(id) { var res =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.