Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making a button click only once

Status
Not open for further replies.

bigfoot

Programmer
May 4, 1999
1,779
US
I am making a download form, and I only want the user able to click the button once.

In the button's onclick event, I do mybutton.enabled=false, but it does not seem to work.

Do I need to do this with JavaScript?

If so, please tell me how to run JS code from a button, and also be able to run my download function.

It seems to be either / or.

Thanks

Gary
 
The mybutton.enabled should work. I suspect that your problem might be with threads (the code may be waiting for the download to complete before trying to set enabled to false or something).
 
Hi bigfoot,

I'm assuming that you want to disable the button DIRECTLY after the user clicks on it, i.e. while the browser is still receiving the response, to stop the user from submitting multiple download requests.

To run javascript from a button, you have to set the HTML 'onclick' attribute. You do this as follows (in C#):

Code:
myButton.Attributes["onclick"] = "disableMyButton();";

You also have to include the javascript function 'disableMyButton' in the page. For example between the <head> and the </head> tags you put the following:

Code:
<script language=&quot;javascript&quot;>
    function disableMyButton()
    {
        document.getElementById(&quot;myButton&quot;).disabled = true;
    }
</script>

This way, when the button is clicked, the javascript function will be executed on the client immediately (so the button will be disabled), and then the request will be made, so the server-side function will be executed also.

regards,
Blaxo
 
ok, that worked, but now it does not run the VB code, just the js code. but it does work.

I even tried this:
myButton.Attributes[&quot;onclick&quot;] = &quot;javascript:return disableMyButton();&quot;



 
myButton.Attributes&quot;onclick&quot; = &quot;javascript:return disableMyButton();&quot; will only stop the form from submitting if the 'disableMyButton' function returns false, so in this case the page should always submit. Have you wired the VB function to the button with the ASP.NET onclick property?

i.e.
<asp:Button runat=&quot;server&quot; id=&quot;myButton&quot; onclick=&quot;myButton_Click&quot; ... />

The important thing to understand is that there are 2 individual 'onclick' attributes:

- The ASP.NET OnClick property (which you set in the <asp:Button> tag), which is used to wire the server-side click event of a button to a server-side VB function;
- The HTML onclick attribute (which you set using the Attributes[&quot;onclick&quot;] call), which wires the client-side click event to a client-side JS function.

You have to set both in order to execute a JS client function AND a VB server function.

Hope this helps,

regards,
Blaxo
 
Here are 2 chunks of my code.

The calling page HTML:
<%@ Page Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; Codebehind=&quot;FileDownload.aspx.vb&quot; Inherits=&quot;WebUtilities.FileDownload&quot;%>
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;>
<HTML>
<HEAD>
<title>frmDownload</title>
<meta content=&quot;Microsoft Visual Studio .NET 7.1&quot; name=&quot;GENERATOR&quot;>
<meta content=&quot;Visual Basic .NET 7.1&quot; name=&quot;CODE_LANGUAGE&quot;>
<meta content=&quot;JavaScript&quot; name=&quot;vs_defaultClientScript&quot;>
<meta content=&quot; name=&quot;vs_targetSchema&quot;>
<script language=javascript>
function disablemybutton()
{
document.getElementById(&quot;btnDownloadCSV&quot;).disabled=true
}
</script>
</HEAD>
<body bgColor=&quot;#ffffcc&quot; MS_POSITIONING=&quot;GridLayout&quot;>
<form id=&quot;Form1&quot; method=&quot;post&quot; runat=&quot;server&quot;>
<asp:button id=&quot;btnDownloadCSV&quot; style=&quot;Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 56px&quot; tabIndex=&quot;1&quot; runat=&quot;server&quot; Height=&quot;24px&quot; Width=&quot;104px&quot; Text=&quot;Download CSV&quot;></asp:button>
....
....
</form>
</body>
</HTML>




The code behind (vb part):

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
btnDownloadCSV.Attributes(&quot;onclick&quot;) = &quot;javascript:return disablemybutton();&quot;
....
....
End Sub

Private Sub btnDownloadCSV_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDownloadCSV.Click
' Call the actual download function
DownLoadFile(eReportFormat.CSV)
End Sub
 
your <asp:Button> tag doesn't include an OnClick attribute. I think it should be as follows:

<asp:button id=&quot;btnDownloadCSV&quot; style=&quot;Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 56px&quot; tabIndex=&quot;1&quot; runat=&quot;server&quot; OnClick=&quot;btnDownloadCSV_Click&quot; Height=&quot;24px&quot; Width=&quot;104px&quot; Text=&quot;Download CSV&quot;></asp:button>

regards,
Blaxo
 
Himmmmm. I never had to have one. I'll test this and see if it works your way.

Thanks.

I just assumed that the .NET studio took care of that sort of thing.
 
I think that's a VB.NET specific thing - automatically wiring events based on the name of a control. Here you need to do it manually it seems.

David
[pipe]
 
&quot;btnDownloadCSV.Attributes(&quot;onclick&quot;) = &quot;javascript:return disablemybutton();&quot;&quot;

The above is actually the preferred way of doing things in .NET, though I've seen it handled like this:

btnDownloadCSV.Attributes.Add(&quot;OnClick&quot;, &quot;disablemybutton();&quot;)
 
Yeah well, its just a matter of choosing between using the indexer or the method ;-)

regards,
Blaxo
 
When I put the onclick in the html, I gat this error message:

Compiler Error Message: BC30390: 'WebUtilities.FileDownload.Private Sub btnDownloadCSV_Click(sender As Object, e As System.EventArgs)' is not accessible in this context because it is 'Private'.
 
I set it to public, and it still will not work.

I put up a message box instead. Giving up.:(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top