Check Modal Popup Extender visibility from code behind

AFAIK, the Modal Popup Extender  has no direct way to determine its visibility state from code behind. This post describes a workaround for that.

The idea here is to wire up handlers that the MPE fires just before it’s about to show the popup (showing event) and before it’s about to hide the popup (hiding event).

In the handlers, we set the property of a HiddenField webcontrol to either ‘1’ or ‘’. We then check the Value property of this HiddenField from code behind. If the MPE has a value of 1, we know the MPE is visible.

   1: Sys.Application.add_load(applicationLoadHandler);
   2:  
   3:  
   4: //Subscribe to the show and hide events of the modal popup.
   5: //Set a hidden field some value when visible and set to empty when hidden
   6: //This hidden field is used in code behind to determine the popup visibility.
   7: function applicationLoadHandler() {
   8:     var mpeEmployeeSearch = $find('mpeEmployeeSearch');
   9:     if (mpeEmployeeSearch) {
  10:         mpeEmployeeSearch.add_showing(employeeShowingHandler);
  11:         mpeEmployeeSearch.add_hiding(employeeHidingHandler);
  12:     }
  13: }
  14:  
  15: function employeeShowingHandler() {
  16:     $get('hfModalVisible').value = '1';
  17: }
  18:  
  19: function employeeHidingHandler() {
  20:     $get('hfModalVisible').value = '';
  21: }

The MPE in this case has been assigned a BehaviorID of mpeEmployeeSearch. The HiddenField WebControl has ID (rendered in HTML) of “hfModalVisible”.

Finally, we can check the MPE visibility from code behind like so:

   1: if (!string.IsNullOrEmpty(hfModalVisible.Value))
   2: {
   3:     //Do something
   4: }

Post a comment if you know of a better way.

1 Comment

Comments have been disabled for this content.