Disable common UpdateProgress controls for an UpdatePanel with an associated UpdateProgress

By · April 12, 2017 · 0 comments

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>

Read more...
ASP.NET Web Forms

Get the latest posts delivered to your inbox.

Comments

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

Leave a Comment

All comments are moderated and rel="nofollow" is in use. Avatars are sourced from gravatar.com – a globally recognised avatar.

Type the numbers from the picture above

About me
John Avis ...mostly about web development and programming, with a little bit of anything else related to the Internet, computers and technology.

profile for John at Stack Overflow, Q&A for professional and enthusiast programmers
Subscribe

Get the latest posts delivered to your inbox. *