Devexpress button Disable button after the first postback occur - asp.net

Hot to disable devexpress button after first postback occur so user couldn't submit it twice.
I have tried to disable it with onClientClick event using javascript but that would also disable the button when some of validators is still not valid too.

Since i use update panel as ajax call. i come across this way.
function pageLoad() {
if (!Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()) {
Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(AjaxBegin);
}
}
function AjaxBegin(sender, args) {
btnxSignUp.SetEnabled(false);
}

Related

Conditional Required Field Validators

I have all fields set to required using the requiredfield control in asp.net but i need one field to change its requiredfield control to disabled depending on a radio button answer. Is there a way to do this without using javascript?
On the radiobutton selection (OnChecked_Changed) event do the setting
if(radiobutton.Checked)
{
requiredFieldValidatorID.Enabled = false;
}
else
{
requirefFieldValidatorID.Enabled = true;
}
Be sure you use validationGroup, Then on the Submit of the page call that.
Page.Validate("validationGroupName");
Then check is page valid to continue e.g.
//call page validation
Page.Validate("validationGroupName");
if(Page.IsValid)
{
// process here
}

ASP.Net web application - server side canceling Submit PostBackUrl if not(!) Page.IsValid

I have a web application, and the submit uses the PostBackUrl to display data if the form is valid.
I need to use different (server side) validation groups, depending on what radiobutton is selected. That is, manually call
Page.Validate('ValidationGroupA').
The validation itself is working fine, in terms of Page.IsValid correctly assigned true or false.
However, if the page is not valid, the page redirects to the page specified by PostBackUrl on the submit button, where the page is found not to be valid, and the client redirected back with:
if (!PreviousPage.IsValid){Response.Redirect("DataEntryPage.aspx");})
However, by getting to here and back again, all form data is lost, and the relevant validation controls and summaries are not displayed.
Is there any equivalent of JavaScripts evt.preventDefault() or some other way, once it is detected validation has failed after a manual call to Page.Validate to post the form back with appropriate validation errors displayed?
Use ASP.Net Client Side Validation.
jQuery Code (call this on onclick of submit button):
$("#SubmitButtonID").click(function (e) {
Page_ClientValidate('ValidationGroupA');
if (!Page_IsValid)
{
e.preventDefault();
}
});
I ended up removing the PostBackUrl directive, and using an onclick event for the submit button
protected void Submit_Click(object sender, EventArgs e)
{
if (chartTypeRadio.Text != "bolus") { Page.Validate("age"); }
if (Page.IsValid) { Server.Transfer("~/PatientSpecificDrugChart.aspx", true); }
}

generate ajax postback for only one updatepannel in javascript

when I click a button the content inside my updatepannel changes. The id of the button is set as a trigger for the updatepannel, so that the other updatepannels are not affected.
How can I replace the buttonclick event with a page load event?
Is there a way to specify the pageload javasript event as a trigger for the updatepannel?
There's no way to register pageLoad() as a trigger, but you can call __doPostBack() in that function to force your UpdatePanel to refresh itself. However, that will cause an infinite loop if you're not careful, because pageLoad() is also called when partial postbacks complete.
One way around that problem is to set a flag before refreshing the panel, and persist that flag in the window object so it survives partial postbacks:
function pageLoad()
{
if (!this.panelRefreshed) {
this.panelRefreshed = true;
__doPostBack("yourUpdatePanelClientID", "");
}
}

validation in asp.net

i am using validators for validation and on linkbutton i am diaplaying popup.
my problem is i want to disable linkbutton means until page is validated means the popup should not be displayed till the page gets validated
<asp:LinkButton ID="LinkButton1" runat="server" CssClass="addProduct-disable" Enabled ="false"
Text="Assign Filter Criteria" CausesValidation="true"></asp:LinkButton>
Try to validate client-side, if possible, via AJAX-Methods.
You may consider using the jQuery event.preventDefault() method.
$('[id$="LinkButton1"]').click(function(event) {
if(! valdiateYourPage() ) {
event.preventDefault();
// display validation errors or something
}
else {
//proceed as normal
}
});
Put your page validation logic in the valdiateYourPage() javascript method. If it valdiates, then process as normal, if not then preventDefault() will stop any further event execution by your LinkButton.

Disable a button on click

I have a button control. Once the user clicks on it, the click event should fire and then the button should get disabled. How can I do this? I have the option to use JQuery or JavaScript or both.
Here is my button declaration:
<asp:Button
ID="Button1"
runat="server"
Text="Click Me"
onclick="Button1_Click"
/>
On the button click code behind, I have added a Response.Write(). That should get executed and then the button should be disabled
For whatever reason, the HTML spec dictates that disabled elements should not be included in POST requests. So, if you use JavaScript to disable the HTML element in the client-side onclick event, the input element will be disabled when the browser assembles the POST request, the server won't be properly notified which element raised the postback, and it won't fire server-side click event handlers.
When you set the UseSubmitBehavior property to false, ASP.NET renders an input element of type button instead of the regular input of type submit that the ASP.NET Button control normally generates. This is important because clicking a button element does not trigger the browser's form submit event.
Instead of relying on a browser form submission, ASP.NET will render a client-side call to __doPostBack() within that button element's onclick handler. __doPostBack will raise the postback explicitly, regardless of what POST data comes through in the request.
With the postback being raised independent of the browser submit event, you're freed of the previously mentioned HTML quirk. Then, you can set an OnClientClick of "this.disabled = true;", which will render as "this.disabled = true; __doPostBack('Button1', '');", and things will work as intended.
add an OnClientClick="this.disabled = true;" to your button.
If you are using Asp.net Ajax you might want to look at using PostBack Ritalin.
Have you tried this?
Add an OnClientClick="MyFunction();" to your .NET button.
Then in the .aspx page script tags you add the following JS function:
<script type="text/javascript">
function MyFunction()
{
window.setTimeout(function ()
{
// get the button/control to disable using your favourite clientside ...
// ... control grabbing code snippet ...
// ... eg. JQUERY $('Button1'), getElementById, etc.)
document.getElementsByName('Button1').Button1.disabled = true;
// I've used "getElementsByName" because .NET will render a button with
// ... a "name" attribute, and not an "id" attribute, by default
}, 1);
}
</script>
This gives the browser a chance to post back, followed by a quick button disable.
You need to be careful that the postback occurs before you disable the button through client script. This is a common gotcha with ajax and input boxes. Disabling an input box prevents the data from being sent from the browser, even if you can see text within it while it is disabled. The answer is that you need to use jquery for this to ensure the server-side code runs first before it is disabled.
-Oisin
// to disable
this.setAttribute('disabled', true);
// to enable
this.removeAttribute('disabled');
this is a cross browser solution
There is really cool event for body tag "<"body onBeforeunload="buttonId.disabled = true;" ">"
This event triggers right before form submits, in other words your data will be submitted correctly.
When using the "this.disabled = true" method make sure you check if the page is valid before disabling the control if you have validators on the page. If validation fails you won't be able to re-enable the control without reloading the page.
if (Page_IsValid) this.disabled = true;
<script type="text/javascript">
Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequestHandler);
function BeginRequestHandler(sender, args) {
document.getElementById('<%= lblMessage.ClientID %>').innerText = "Processing...";
document.getElementById('<%= btnSubmit.ClientID %>').innerText = "Processing";
args.get_postBackElement().disabled = true;
}
</script>
Add Script Tag in source page . change Id of button in code . You can disable the button till the process completes execution .
you can disable it server side
Button1.Enabled = false;

Resources