Check Modal Popup Extender visibility from code behind - Raj Kaimal

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.

Published Tuesday, May 05, 2009 12:41 AM by rajbk

Comments

# Using the Modal Popup Extender to build a popup search interface

This post and sample code demonstrates how to use the Modal Popup Extender (MPE) to display a popup search

Thursday, May 07, 2009 2:30 AM by Raj Kaimal

# re: Check Modal Popup Extender visibility from code behind

Thanks for this! Very helpful!

The only thing to note is that I had to include your javascript code after the ending </form> tag so that the "Sys" object is initialized before the js attempts to access it.

I think this means that you code won't work from inside a content page. Any suggestions on tailoring it to work with content pages?

Thursday, December 10, 2009 10:33 AM by Kiley Naro

# re: Check Modal Popup Extender visibility from code behind

For content pages, use Page.ClientScript.RegisterStartupScript

Wednesday, May 26, 2010 6:57 AM by ez

# How can I know ajax modal popup is opened? - Programmers Goodies

Pingback from  How can I know ajax modal popup is opened? - Programmers Goodies

Leave a Comment

(required) 
(required) 
(optional)
(required)