Disable common UpdateProgress controls for an UpdatePanel with an associated UpdateProgress

John Avis 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.
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

Computers & Internet Web Development Website Hosting

500 Internal Server Error after migrating from IIS 7.5 to IIS 10

by John Avis | November 4, 2019

As support ends for Microsoft Windows Server 2008 I have recently gone through migrating some websites to a new server running Windows Server 2016 and IIS 10 but some of the websites did not work.


Jquery/Javascript Web Development

tagInput: A simple jQuery plugin for tag entry using Bootstrap 4

by John Avis | October 15, 2019

For a website project I needed a way to enter multiple tags. I just wanted something simple that I could easily modify to suit my own needs, so I wrote my own.


ASP.NET Web Forms Web Development

ASP.NET bug with RadioButton GroupName in Repeater

by John Avis | September 1, 2019

I rediscovered a bug in ASP.NET that affects RadioButtons inside repeaters. Here is my solution to the problem.

Comments

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

Leave a Comment

Tags

About me

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

Subscribe

Get the latest posts delivered to your inbox.