Instantly disable Button on click - c#

I'd like to instantly disable a Button after clicking it, so a User can't click it twice in short succession and fire the OnClick_Event twice in a row. btn.Enabled = false doesn't seem to do the trick instantly. Is there any other way to accomplish this?
Thanks,
Dennis

What you are doing is disabling it after a post back therefore your button will be disabled in the page that's rendered when the browser receives the response.
Do it on the client-side with JavaScript instead:
var button = document.getElementById('yourButton');
button.disabled = true;
If you're facing issues with posting back to the server, check out this article: How to disable an ASP.NET button when clicked.

function disable()
{
var button = document.getElementById('<%= Button1.ClientID %>');
button.disabled = true;
}
<asp:Button ID="Button1" OnClientClick="disable();">

you can achieve this via javascript
function Disable(btn)
{
btn.disabled = true;
}
and add this to your button OnClientClick="javascript:return Disable(this);"

You can handled it simply by this code.Suppose, here is a button named btnAdd.When you click this button it will be disable due to execute it's corresponding code.I mean due to post back it will be disabled.
if (!IsPostBack)
{
btnAdd.Attributes.Add("onclick", "this.disabled=true;" + GetPostBackEventReference(btnAdd).ToString());
}

Write the btn.Enabled= false at the end of all the functionality in the Onclick Event of the Button. It will do the Required action.

Disabling the button has raised on problem. You won't get the server side event fired. So better seek the alternative by hiding the button (and yes displaying some friendly error message 'working', 'processing' etc.). The work out on how to disable asp.net button on postback would help. Thanks.

YourButton.Enabled = false;
if(!YourButton.Enabled)
{
\\Your Code Here
YourButton.Enabled = true;
}
Hope this helps ;)

Related

Dynamically Adding AsyncPostBack Triggers

In the Page_Load section of my code I am dynamically creating a form. Each row has a save button at the end and I want the form to asynchronously post back when someone clicks the "save" button at the end of a row. I can't seem to achieve this though. I click the button and nothing happens. The click event is not even firing so my guess is that the trigger is not being added properly. Any ideas how I can get this functionality working?
Here is a snippet of my code that I believe is where the problem lies.
Button submit = new Button();
submit.ID = "Submit_" + reader["SEQN"];
submit.Text = "Save";
submit.Click += submitTest;
AsyncPostBackTrigger apt = new AsyncPostBackTrigger();
apt.ControlID = submit.UniqueID;
UpdatePanel1.Triggers.Add(apt);
A couple things i see. Set the trigger Event to the event you want executed: apt.EventName = . The second thing is the id of the button correct after final page rendering. Check the Buttons id value from web inspector or firebug and see if its the same as what you set for controlid.

CausesValidation not working with custom validators

I have custom validators on the aspx page and user needs to see the error message in summary as soon as they tab out of the control.
To achieve this, I am calling Page_ClientValidate('') on onblur event of each control. One of the custom validator I have is:
function ValidateCustomerId(oSrc, args) {
var txtCustomerId = document.getElementById(txtCustomerId);
if (txtCustomerId != null) {
var customerId = txtCustomerId.value;
if (!isInteger(customerId)) {
document.getElementById("customerIdAsterik").style.display = 'inline';
args.IsValid = false;
}
else {
document.getElementById("customerIdAsterik").style.display = 'none';
}
}
}
If user enters invalid entry and clicks on Cancel button, server side event is not getting fired untill it is clicked twice. Cancel button already has CausesValidation=false. I think this behaviour is due to calling on Page_ClientValidate() on onblur event, otherwise it works fine.
Is there to skip the client validations when they click on cancel button or is there any approach I could take to achieve this.
Similar kind of issue is listed here:
http://weblogs.asp.net/gurusarkar/archive/2013/05/30/after-first-postback-why-i-have-to-click-the-button-twice-for-postback-to-occur.aspx
Not sure if that applies but the Validation is fired due to the onblur.
So I think setting that Page_Block=false in the button click might work.

enable button in C# by removing 'disabled' property using firebug

I have a question about disable button (C#) that I need your help.
in Asp.net (C#), disable the button code:
Button btn = new Button();
btn.click += new EventHandler(btn_click);
btn.Enabled = false;
btn.UseSubmitBehavior = false;
The generated HTML:
<input type="button" disabled="disabled">
The button is disabled until I use firebug and remove the 'disabled' property and change the type property to submit (<input type="submit">) then user can click on btn and it can perform btn_click method(postback).
How could we prevent this issue? I try to use CommandName (disable, enable) to mark disable button and inside btn_click method, I check if CommandName== "disable" then stop the function but it is very messy.
Thanks for your help.
Van
Client can remove the Disabled attribute but cannot change the control ViewState. On server side you can do that:
protected void btn_Click(object sender, EventArgs e)
{
if (btn.Enabled)
{
// do something
}
}
<asp:Button ID="btn" runat="server" Enabled="false" OnClick="btn_Click" Text="Test" />
You can't control your users' behavior.What you need to do is handle these things in server-side..
you can judge the button's status in btn_click.. and then do some work
#bystander is right. You need to check on the server whether that condition is correct. You can't rely on the client being 100% fool proof, and that's the exact reason why.
Can you hide the control instead, and reenable it on some future postback? Hiding doesn't actually render the control to the client at all.

asp.net stop button event running on Refresh

I have a button that has click event in my C#. This works great. However afetr pressing F5 to refresh the page I noticed the button event is fired. This occurs every time I click F5. I've added if(page.IsPostBack) and the method still runs.
Is there any way to stop this method from firing when the F5 Refresh button is clicked?
Thanks!
http://pastebin.com/9ycNraaT
try to use Response.Redirect("Your page ") after the your add/update code. it isn't good practice, but it will help you.
if(!page.IsPostBack)
NOT if(page.IsPostBack)
If you have a page that needs to be run once, you should implement logic in your code that prevents the same method from running multiple times. In pseudo-code:
if (canExecuteMethod()) {
executeMethod();
} else {
displayMessageThatMethodCannotBeExecuted();
}
For example, you can check a Session variable with canExecuteMethod(), which returns false when it is not present and is set in executeMethod():
function canExecuteMethod() {
return Session["methodIsExecuting"] == null
|| Session["methodIsExecuting"] == false;
}
function executeMethod() {
Session["methodIsExecuting"] = true;
// ...
}
Of course the use of a Session variable has it limits, so you might want to consider using a database or AppSettings variable instead.
No way. If once you click button (and post data) after refresh the same data will be sended.

ASP.NET - How to track event from previous postback?

I have a requirement to check for a certain condition on postback before redirecting (Response.Redirect) to another page.
Note... I cannot use JavaScript to detect whether or not to confirm (this is also a requirement) :s
Pseudo:
protected void lbtnRedirect_OnClick(object sender, EventArgs e)
{
if (showConfirm)
{
// Set flag for client side
this.ShowConfirm = true;
// Track this event for next postback.
}
else
{
Response.Redirect("somepage.aspx");
}
}
If the showConfrim flag == true, then the client will be show a modal dialog box asking them if they are sure they want to redirect. If the user clicks on "Yes", then the page posts back and the desired effect is that the lbtnRedirect_OnClick event is fired. How would I about tracking the lbtnRedirect event?
Edit:
I have no problem tracking the flag to show the modal (yes JS must be used to show the modal... somethings you just cannot get rid of :)). I should have been more clear.
It is when the user clicks "Yes" to continue the redirect. The page will postback again but needs to know which event to go through.
i.e. Suppose there are 3 onclick events, 1) lbtnRedirect1_Onclick 2) lbtnRedirect2_OnClick 3) lbtnRedirect3_OnClick... each of which does the confirm check.
Each onclick event does the check. So when the user clicks on "Yes" on the modal, how does the page know which event to drop back into?
You can use ViewState if you're in WebForms.
Implement a ShowConfirm property encapsulating ViewState["ShowConfirm"].
In the first postback you'll set ShowConfirm 'true', and this will activate that modal during the render (if ShowConfirm is true, that's setting as visible 'true' some control).
In the next postback, you'll set ShowConfirm 'false' because is 'true', and finally you'll do the whole redirect!
You can use an ajax call from javascript to set the required values.
Since the postback will happen before even the execution reaches to your button click event we need a workaround here, And if you don't need JS as your requirement, so take a look at
Implementing Client Callbacks Programmatically without Postbacks in ASP.NET
This is much like a wrapper for XMLHttp Ajax call IMHO.
You cannot easily create a model form, without javascipt.
One suggestion I would make is to have panels in your page.
Panel one is visible.
On submit one; panel one hides and panel two is visible asking for a confirmation.
On panel two is a confirm button, clicking this button your redirection is performed.

Categories