Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Web Controls (http://www.velocityreviews.com/forums/f63-asp-net-web-controls.html)
-   -   Disable Button after click it (http://www.velocityreviews.com/forums/t777562-disable-button-after-click-it.html)

Guzman 01-05-2006 10:32 PM

Disable Button after click it
 
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!


Phillip Williams 01-05-2006 11:31 PM

RE: Disable Button after click it
 
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!
>


Stephan Steiner 01-11-2006 04:26 PM

Re: Disable Button after click it
 
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



Wouter van Vugt 01-11-2006 06:47 PM

Re: Disable Button after click it
 
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


Wouter van Vugt 01-11-2006 06:52 PM

Re: Disable Button after click it
 
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


Wouter van Vugt 01-11-2006 07:05 PM

Re: Disable Button after click it
 
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);
}
}


Wouter van Vugt 01-11-2006 07:09 PM

Re: Disable Button after click it
 
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



All times are GMT. The time now is 11:05 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.