Disable common UpdateProgress controls for an UpdatePanel with an associated UpdateProgress

johna by | April 12, 2017 | ASP.NET Web Forms Web Development

If you have an ASP.NET Web Forms page with multiple UpdatePanels and multiple UpdateProgress controls, sometimes you may want one or more UpdateProgress controls associated with a specific UpdatePanel, and one or more UpdateProgress controls that have no association with an UpdatePanel so are shown when any UpdatePanel is updated.

However, to me it makes sense that if an UpdatePanel has an associated UpdateProgress control, then any other UpdateProgress controls should not be displayed.

Unfortunately this is not how it works in ASP.NET. All unassociated UpdateProgress controls will be displayed, even if there is an associated UpdateProgress.

If you want to override how it works by default, and instead not display UpdateProgress controls if an UpdateProgress control exists that is associated with the UpdatePanel that triggered the update, then the following client-side script can be used.

The script should be placed or included after the ASP.NET ScriptResource.axd files are included. It's safe to place or include this script after the closing ASP.NET tag.

Note that this script will not function if a partial post back is triggered by a control outside of any UpdatePanel. You could modify the script to accommodate this if needed.

 <script type="text/javascript">

//array of original values
var updateProgressDefaults = [];

Sys.Application.add_init(appl_init);

function appl_init() {

var c = Sys.Application.getComponents();
for (var i = 0; i < c.length; i++) {
if (Object.getType(c[i]).getName() == "Sys.UI._UpdateProgress" && !c[i]._associatedUpdatePanelId) {
updateProgressDefaults[c[i].get_id()] = c[i]._associatedUpdatePanelId;
};
}

var pgRegMgr = Sys.WebForms.PageRequestManager.getInstance();
pgRegMgr.add_initializeRequest(BeginHandler);
pgRegMgr.add_endRequest(EndHandler);
}

function BeginHandler(sender, args) {
//There is no _updatePanelsToUpdate if a trigger is outside of an UpdatePanel
if (!args._updatePanelsToUpdate) return;

//Check if the affected UpdatePanel has an associated UpdateProgress control
var hasUpdateProgress = false;

var c = Sys.Application.getComponents();
for (var i = 0; i < args._updatePanelsToUpdate.length; i++) {
for (var j = 0; j < c.length; j++) {
if (Object.getType(c[j]).getName() == "Sys.UI._UpdateProgress" && args._updatePanelsToUpdate[i].replace(/$/g, "_") == c[j]._associatedUpdatePanelId) {
hasUpdateProgress = true;
break;
}
}

if (hasUpdateProgress) break;
}

//If the UpdatePanel has an associated UpdateProgress control then set all the UpdateProgress controls without associated UpdatePanels to a non-existant UpdatePanel
if (hasUpdateProgress) {
for (var i = 0; i < c.length; i++) {
var id = c[i].get_id();
var type = Object.getType(c[i]).getName();
if (type == "Sys.UI._UpdateProgress" && !c[i]._associatedUpdatePanelId) {
c[i]._associatedUpdatePanelId = "NullUpdateProgress";
};

}
}
}

//Restores AssociatedUpdatePanelId for all UpdateProgress controls
function EndHandler() {
for (var key in updateProgressDefaults) {
$get(key).control._associatedUpdatePanelId = updateProgressDefaults[key];
}
}
</script>

Related Posts

Web Development Retro Computing

Converting dBase IV programs to run in the browser

by johna | September 13, 2024
Some pointless entertainment trying to get some old dBase programs running in the browser.

Web Development

How to set up a debugging using the Turnkey Linux LAMP stack and VS Code

by johna | December 19, 2023
The second part in my guide to setting up a website and database using the Turnkey Linux LAMP stack.

Website Hosting Web Development

How to set up a website and database using the Turnkey Linux LAMP stack

by johna | November 18, 2023
If you need to host your own website for the purposes of web development, Turnkey Linux LAMP Stack is an easy to install all-in-one solution that you can set up on a spare computer or a VM (Virtual Machine).

Comments

There are no comments yet. Be the first to leave a comment!

Leave a Comment

About

...random postings about web development and programming, Internet, computers and electronics topics.

I recommend ASPnix for web hosting and Crazy Domains for domain registration.

Subscribe

Get the latest posts delivered to your inbox.