disable/enable buttons - c#

I'm working in ASP .Net. I need to be able to click a button, disable it, have the code behind kick in and execute some functions and once they are done, enable the button again. There is no postback taking place per say in this project as we are using an UpdatePanel. About as far as I've been able to get successfully is using this syntax to disable it on the code behind Page_Load function:
btnConnect.Attributes.Add("onClick", "document.body.style.cursor = 'wait';this.disabled = true;" + ClientScript.GetPostBackEventReference(btnConnect, string.Empty) + ";");
This disables the button and allows the code behind to execute. I just can't figure out how to re-enable the button once the functions that run in the code behind are done. Is there a way to capture when the code behind is done?

Are you able to use jQuery instead? Consider a flow like this:
on click, disable button
call/POST to an ASP.NET URL as needed
re-enable the button when the URL call returns. Update page elements, redirect, alert as needed.

You can use a timeout approach after the disable:
var that = this; //pointer to button
window.setTimeout(function() { document.body.style.cursor = '';that.disabled = false; }, 1000);
This approach can be useful to prevent a double-postback too.
Or the update panel finishes it fires the Sys.WebForms.PageRequestManager.endRequest event. Add an event handler to reenable the button in there as another alternative.

In addition to above...
Yes, you are using an UpdatePanel, but a PostBack is occurring.
So, in your submit button event, after successful processing is complete, remove the attribute for onClick you added above, have you tried to enable the button in the code behind and then issue an Update() on your UpdatePanel?

Related

How to call a full postback after the execution of the button click event inside an user control?

Okay,
I have this scenario:
There is a user control with an update panel within it. There is a button within that update panel with proper postback trigger being set. The button_click event is also defined well. I need to call a full postback of the parent aspx page once the "button_click" event is completed. Under ideal case, all the form submission events such as postbacks occur before event based methods are executed. This means my page will first be reloaded then the button click event will be executed. I want something like to reverse this operation. First Button_click event execution then one postback after that on the aspx page(this page calls the user control-> and this user control has the updatepanel with button in it).
Any possible way out would be highly appreciated.
I don't think there's a way to change ASP.NET's lifecycle, like the one you described. A (dirty) way of postbacking the parent page is however to put a hidden button on that page, and call it via javascript in the UC. (via ScriptManager.RegisterStartupScript)

How can you disable a.NET ImageButton control on the client side when the ImageButton has an onClick event handler?

The purpose here is to prevent users from clicking the button multiple times during the submit/click. I've tried the different solutions for a Button control but they do not apply to an ImageButton.
Try the following code:
imgbutton.OnClientClick = "this.disabled = true;";
This will disable the button at the client side as soon as it is pressed. (The syntax may be off - I can't test this code right now.)

Run Javascript function after postback in asp.net

I have a button which causes a postback and also calls the javascript function hideInsert() which looks something like this:
function hideInsert() {
$('.hide').hide();
alert("hide");
}
All it does is hiding tablerows marked with ".hide". This works as intended but since the postback occurs, everything gets reset.
Is there anyway I can click the button to trigger the postback and then run the function, after the postback has occurred?
I have been looking at this http://msdn.microsoft.com/en-us/library/z9h4dk8y.aspx but with no success.
I would then need to press another button which would trigger the showInsert() function, which would need a similar function.
In whatever event makes most sense according to your current architecture, include:
if (Page.IsPostBack) {
ClientScript.RegisterStartupScript(this.GetType(), "HideOnPostback", "$(function() { hideInsert(); })", true);
}
Page_Load is a common place to include logic like this.
Alternatively, if you will never need whatever is classed as .hide after they postback and they are server-side controls, you could always set them to Visible = false.

updating updatepanel's based on texbox focus lost in asp.net

I have an asp.net page with two user controls. Each of which are in separate updatepanel's One user control has three textboxes. I want to update the second updatepanel based on change of text/ focus out in first user control. How can I access both user control's textboxes, and other controls in page and update the updatepanel on change of text ?
updatepanel1
user control1
textbox1
textbox2
textbox3
updatepane2
usercontrol2
label1
Regards,
Asif Hameed
Whoa, UpdatePanels! That takes me back. Triggering UpdatePanels to "postback" asynchronously from the client has always been a bit of a kludge. The common way was to register an AsyncPostBackTrigger with a hidden button's click event and then explicitly call its click event on the client side. However, a solution with a few less layers of indirection is to call the ASP.NET AJAX library's __doPostback() JS function.
Assuming you're using jQuery (which may be far-fetched considering you're still using UpdatePanels!), you can add an event handler to the 'focusout' event of your UserControl1 to trigger the asynchronous postback of your UpdatePanel2. I would recommend putting this JS outside of one of your UpdatePanels.
$('#userControl1').on('focusout', function() {
__doPostback('UpdatePanel2UniqueId', '');
});
I dug up a good article that explains the technique of using __doPostback in a bit more detail.
Easily refresh an UpdatePanel, using JavaScript

ASP.Net & jQuery - closing a popup

I want to close a ShadowBox popup when the user clicks a button. The popup is showing a separate page in an Iframe.
This works fine with a clients-side event on the Cancel button control, e.g.
OnClientClick="javascript:parent.Shadowbox.close();"
The Ok button, however, has a server-side event, because data needs to be saved. When I define both an OnClick and the OnClientClick handler from above, the IFrame is closed and the server-side event handler never fires.
I tried to remove the OnClientClick event handler from the markup and to use the ClientScriptManager to accomplish this, as in
Page.ClientScript.RegisterStartupScript(this.GetType(),
"Close", "parent.Shadowbox.close();", true);
Apparently because the buttons are in an UpdatePanel, the script does not get registered, and it does not appear in the Reponse stream. The IPanel stays open.
How can I do this?
When you're using the MS AJAX controls, you need to register your scripts with the ScriptManager, not ClientScript.

Categories

Resources