Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Disable Button after click it

Reply
Thread Tools

Disable Button after click it

 
 
Guzman
Guest
Posts: n/a
 
      01-05-2006
Hi, i want to disable a button after i click it, so the button it is not
submited twice.
The thing is that i'm working with the onclik event in the server side, and
in the server side i disable the button, but sometimes the users click the
button twice really fast, and i don't want this to happen.
Does someone know how can i disable the button on the client side??
Can i Submit button both ' runat=server ' and Disable OnClick ? because i
been trying this but its not working...

Thanks for any help!

 
Reply With Quote
 
 
 
 
Phillip Williams
Guest
Posts: n/a
 
      01-05-2006
There are several work arounds. My own solution is to place 2 buttons on the
form; one is visible and the other is hidden like this:

<input id="btnPreSubmit" type="button" value="Submit"
onclick="SubmitAndDisable();">
<asp:button id="btnSubmit" Runat="server" CssClass="Hidden"
CausesValidation="True"></asp:button>

The hidden class should be defined in your styles section as this:
..Hidden
{
display:none;
}

When the user clicks on the visible button the following javascript executes
the Click event of the real button and disable the displayed button:

<script language="javascript">
function SubmitAndDisable()
{
var btn = document.getElementById("btnPreSubmit");
btn.disabled = true; //this will disable the button that is visible to the
user
btn = document.getElementById("btnSubmit");
btn.click(); //this will execute the real submit button
}
</script>

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"Guzman" wrote:

> Hi, i want to disable a button after i click it, so the button it is not
> submited twice.
> The thing is that i'm working with the onclik event in the server side, and
> in the server side i disable the button, but sometimes the users click the
> button twice really fast, and i don't want this to happen.
> Does someone know how can i disable the button on the client side??
> Can i Submit button both ' runat=server ' and Disable OnClick ? because i
> been trying this but its not working...
>
> Thanks for any help!
>

 
Reply With Quote
 
 
 
 
Stephan Steiner
Guest
Posts: n/a
 
      01-11-2006
How about lock(this) when you enter the method handler (to prevent a second
fired event can enter the method), then you set a variable that prevents the
event handler from processing any further events. It's a completely
different approach, but the end result ought to be the same.

Regards
Stephan


 
Reply With Quote
 
Wouter van Vugt
Guest
Posts: n/a
 
      01-11-2006
How about normal Javascript. You're problem is that a user submits a
page two times really fast. No server side code will be able to stop
this. What you need is a button which also renders some javascript to
disable it when it has been clicked. In HTML it would look something
like:
<script type='text/javascript'>
function DisableAndSubmit(buttonID)
{
var btn = document.getElementById(buttonID);
btn.disabled = disabled; // test this one, I don't know the
javascript entirely by heart
__doPostBack(buttonID...) // this line you can obtain using either
Page.GetPostbackEventReference, or
Page.ClientScript.GetPostbackEventReference
}
</script>
<input type='button' id='myButton'
onclick='DisableAndSubmit('myButton')'>


Grtz,

Wouter van Vugt
Trainer - Info Support
http://blogs.infosupport.com/wouterv

 
Reply With Quote
 
Wouter van Vugt
Guest
Posts: n/a
 
      01-11-2006
Hi Stephan,

I am sorry if I sound a bit rude. But I believe that is a very bad
idea. This will probably severely hamper your performance, presents the
possibility
for deadlocks, and it is what I call a hack, not a fix. Never use the
lock keyword for anything else but thread synchronisation, or better,
avoid
needing it all together if you can. Always examine the problem, not the
symptom, and trace the problem to its source, apply the fix there, or
in the future, it will really bite you.

Still, happy coding!

Grtz,

Wouter van Vugt
Trainer - Info Support
http://blogs.infosupport.com/wouterv

 
Reply With Quote
 
Wouter van Vugt
Guest
Posts: n/a
 
      01-11-2006
Sry for the ugly formatting, I decided to just build it. I think it is
all ASP.NET 1.1 (built with VStudio 2005...)

public class SingleClicker : WebControl
{
[DefaultValue("")]
public string Text
{
get
{
string text = (string)ViewState["Text"];
if(text == null)
{
text = String.Empty;
}
return text;
}
set { ViewState["Text"] = value; }
}

protected override HtmlTextWriterTag TagKey
{
get
{
return HtmlTextWriterTag.Input;
}
}

protected override void AddAttributesToRender(HtmlTextWriter
writer)
{
writer.AddAttribute(HtmlTextWriterAttribute.Onclic k,
String.Format("DisableButtonAndPostBack('{0}')",
ClientID));
writer.AddAttribute(HtmlTextWriterAttribute.Type,
"button");
writer.AddAttribute(HtmlTextWriterAttribute.Value,
Text);
base.AddAttributesToRender(writer);
}

protected override void OnPreRender(EventArgs e)
{
if (Page.IsClientScriptBlockRegistered("SingleClicker ") ==
false)
{
Page.RegisterClientScriptBlock("SingleClicker",
@"<script type='text/javascript'>
function DisableButtonAndPostBack(buttonID)
{
var btn = document.getElementById(buttonID);
btn.disabled = ""disabled"";" +
Page.GetPostBackEventReference(this) + ";" +
@"
}
</script>");
}
base.OnPreRender(e);
}
}

 
Reply With Quote
 
Wouter van Vugt
Guest
Posts: n/a
 
      01-11-2006
The Click event needs implementing. I left this for ya'll as a nice
practive

Grtz,

Wouter van Vugt
Trainer - Info Support
http://blogs.infosupport.com/wouterv

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disable submit button after user click GS ASP .Net 11 05-14-2011 02:06 PM
Disable button after first click Sinity ASP .Net 15 09-04-2007 09:47 AM
Disable submit button after click but fire validators? Chris Ashley ASP .Net 1 12-16-2005 11:39 AM
Disable button after first click Sinity ASP .Net 0 11-12-2003 05:35 PM
Disable button after first click Sinity ASP General 1 11-12-2003 05:17 PM



Advertisments