ASP.NET AJAX UpdatePanel - preventing postback in InitializeRequest will not reset back to enabled state

In my ASP.NET AJAX UpdatePanel, there is a submit button with custom javascript validation that sets a global flag called _cancelGeneralSettingsUpdate based on the validation results.

Everything works fine except when an invalid value is entered, corrected, and resubmitted. Even though the variable _cancelGeneralSettingsUpdate is set to false, the initializeRequest function is not triggered before the page tries to postback via AJAX, resulting in a failed postback.

I need to ensure that postbacks are successful once the user corrects invalid input and submits again. Unfortunately, it seems like I am not properly connected to the PageRequestManager pipeline after cancelling the postback. If I refresh the page, everything starts working again, but cancelling the postback using args.set_cancel(true); puts me in a state where I cannot make further postbacks via AJAX until the page is refreshed.

Is there a way to re-enable postbacks or trigger the initializeRequest function to fire and set this before subsequent postbacks?

Below is some of the code I have. Please assume that _cancelGeneralSettingsUpdate is correctly set before any postback attempts, as I have verified this.

var _cancelGeneralSettingsUpdate = false;

function initializeRequest(sender, args) {
        var pageRequestManager = Sys.WebForms.PageRequestManager.getInstance();
        if (!pageRequestManager.get_isInAsyncPostBack() & args.get_postBackElement().id == '<%= UpdateWorkgroupGeneralButton.ClientID %>')
        {
            if (_cancelGeneralSettingsUpdate) {
                args.set_cancel(true);
                // Show error message
            }
            else {
                args.set_cancel(false);
            }
        }
        else {
            args.set_cancel(false);
        }
    }

Answer №1

Realization I thought I had it figured out. My solution involves triggering this function when my JavaScript validation doesn't pass muster.

 Sys.WebForms.PageRequestManager.getInstance().abortPostBack();

In the scenario mentioned above, I was utilizing args.set_cancel(false); within a customized initializeRequest handler. Initially, this method worked like a charm for the initial postback attempt. However, it fell short when subsequent postbacks were required following user error corrections and form resubmission.

Ultimately, this action only halts postbacks that are currently in progress. In my situation, __doPostBack(...) hadn't been invoked yet, causing

Sys.WebForms.PageRequestManager.getInstance().abortPostBack();
to fail without any visible outcome.

The end result? Resorting to a dual-button approach. Given the limitations of the AJAX library, this proved to be the most viable alternative.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The defined function in Node.js did not work properly with the callback

This code snippet demonstrates how to use the findOne() method with Node.js and MongoDB. var MongoClient = require('mongodb').MongoClient; MongoClient.connect('mongodb://localhost:27017/blog', function(err, db) { if(err) throw er ...

The jsTree rendering does not properly display the "closed" state of the JSON data nodes

Currently, I am in the process of setting up a jsTree instance to display a directory listing from a file server. However, I am encountering challenges with getting "sub-folder" or "sub-directory" nodes within the jsTree to show as open-able. Even though ...

Verify Departure on External Links within Wordpress

Let me set the stage for you: We're dealing with a bank website here. So, whenever a user wants to click on an external link (could be to a Facebook page or a partner website), we need to have a notification box pop up saying "You are about to lea ...

Alternate routing based on conditions in Angular

I've used the "$urlRouterProvider.otherwise('{route here}')" syntax in angular to create a catch-all route in Angular UI-Router. One thing I'm curious about is whether it's possible to have conditional "otherwise" routing based o ...

The issue with ng-change is causing the previously selected drop down option to be cleared

After successfully implementing live searching with ng-change, I encountered an issue with a pre-selected drop-down box. Despite setting the selected="selected" attribute to make option three the default selection, the drop-down box jumps to the top optio ...

Grid layout with card tiles similar to Google Plus

I'm looking to develop a scrolling grid with card tiles similar to Google Plus that has three columns. I am currently using Material UI, but I can't seem to find the right functionality for this. I have experimented with the standard Material UI ...

"Collaborative Drive" assistance within Google Apps Script

Currently, I am developing a JavaScript tool within Google Apps Script to analyze various document properties such as the validity of links and correct permissions settings. To achieve this, I have been utilizing the API outlined in https://developers.goog ...

Encountering difficulties in populating mongoose document following a model query

Hi everyone, this is my first time posting on SO so I'm hoping for some positive outcomes. I've been using Mongoose in a current project without any issues until now. The problem arises when trying to populate object references after querying fo ...

Managing the state of dynamically generated tabs within a NextJS application

Looking to develop a web app in Next.js that includes tabs components. The goal is to manage various entities within each tab, such as utilizing a search bar to select different products. Upon selecting a product, a new tab will be generated with the produ ...

Issue with Jest mock function failing to trigger axios instance function causing it to return undefined

I initially found a solution on this StackOverflow thread However, I wanted to add my own helper function that generates a fresh Axios instance with the user's authentication token. Here is what I came up with: import axios from "axios"; c ...

Display only specific PHP-encoded JSON data in a formatted table

After receiving a variable from PHP, I convert it to JSON as shown below: var myData = <?php echo json_encode($json_array) ?>; When I log the output, it looks something like this: 0: Carat: "0.70" Clarity: "VVS2" Color: "D" Cut: "Very Good" Polish ...

Using Express.js with synchronous functions can cause the web application to freeze

While developing an app using Express, I realized that I made a mistake regarding my usage of Promises. Here is the code in module.js: module.exports = function(arguments){ for(let k =1; k < something.length; k++){ let options = { & ...

Issue with ASP.NET, VB.NET, and Database Integration

Currently, I am delving into the world of .NET frameworks to improve my skills for work and personal growth. However, I seem to have hit a roadblock as things are not progressing smoothly. The error seems to be stemming from this particular snippet: ...

Utilizing React Typescript Discriminating Unions to choose between two different types based solely on props

In my project, I have a component that consists of different types: type Base = { color: string } type Button = { to: string } & Base type Link = { link: string linkNewTab: boolean } & Base type ComponentProps = Button | Link e ...

What is the best way to duplicate a complete row?

I am looking to create a form that includes an "Add Row" button. When this button is clicked, a new row should be generated, identical to the last row. The rows contain dropdown values, and I want the new row to display the same dropdown options as the p ...

The database server is not appearing on the main.js page of the client

My client's main.js file contains code that is meant to display previous form entries on the webpage, but after submitting the form, no entries appear on the HTML page. My server is running on port 7777 and the root route works in Postman, as does the ...

Node.js encounters a conflict between route names and directory names

Can you use the same name for both a route and a directory in a Node.js project? For example: require("./test")(router); And inside the test.js file: app.get("/test", function(req, res) {}); Also, test is a directory in the same project. When attempti ...

Guide to ensuring jQuery Ajax requests are fully processed with WatiN

Currently, I am in the process of developing WatiN tests to evaluate an Ajax web application. However, I have encountered a timing issue with Ajax requests. My main objective is to ensure that WatiN waits for the Ajax request to be completed before valida ...

Passing JSON data from an ASP.NET controller to a view

My issue arises when a web page is loaded and triggers a controller action to retrieve data based on user selection. I am trying to return this data as a JSON object, but it appears as a single string within the HTML page. The basic structure of the contro ...

I am able to see the Redux props showing up in Redux DevTools, but for some reason they are not

I am facing an issue where I am passing a Redux prop to a component that is fetched from a payload. The payload successfully updates the state in my reducer and the component receives the prop (an array of objects fetched in the action) according to my Red ...