title: Modal popup during page load not hiding sometimes: ASP.NET C# Javascript Jquery
body: <p>I have an ajax modal pop-up that is being displayed during page load and then it hides when the page load has been completed, all of this code takes place within the master page. The problem is that for some button clicks they do not trigger the javascript event window.onload to be called or the pageload complete or render events within asp. They only call the before unload event and submit events. When this happens I notice that the popup does not hide because no window.onload was ever called. Otherwise it works wonderfully. Does anyone know a way that I can make sure the pop up gets hidden always without having to hide the pop up on every button clicks code that does not reload the entire page? See below for code.</p>
  
  <p>Master Page:</p>
  
  <pre><code>&lt;%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Site.master.cs" Inherits="OnlinePrizing.SiteMaster" %&gt;
  &lt;%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %&gt;
  
  &lt;!DOCTYPE html &gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"&gt;
  &lt;head runat="server"&gt;
      &lt;title&gt;&lt;/title&gt;
      &lt;link href="~/Styles/Site.css" rel="stylesheet" type="text/css" /&gt;
  
             &lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;script src="http://code.jquery.com/jquery-latest.js" type="text/javascript"&gt;&lt;/script&gt;
  &lt;script type="text/javascript"&gt;
      function showPopup(who) {
          console.log("show called: " + who);
          var modalPopupBehavior = $find('programmaticModalPopupBehavior');
          modalPopupBehavior.show();
      }
      function hidepopup(who) {
          console.log("hide called: " + who);
          var modalPopupBehavior = $find('programmaticModalPopupBehavior');
          modalPopupBehavior.hide();
      }
  
      window.onbeforeunload = function () {
          showPopup('window.onbeforeunload');
      };
  
      window.onsubmit = function () {
          showPopup('window.onsubmit');
      };
  
      window.onload = function () {
          hidepopup('window.onload');
      };
      &lt;/script&gt;
      &lt;style type="text/css"&gt;
  .modalBackground {
      background-color:Blue;
      filter:alpha(opacity=30);
      opacity:0.3;
  }   
  .modalPopup {
      background-color:white;
      border-width:3px;
      border-style:solid;
      border-color:Gray;
      padding:3px;
      width:250px;
  }
  &lt;/style&gt;
      &lt;asp:ContentPlaceHolder ID="HeadContent" runat="server"&gt;
      &lt;/asp:ContentPlaceHolder&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;form runat="server"&gt;
      &lt;asp:ToolkitScriptManager ID="ToolkitScriptManager1" EnablePartialRendering="true" runat="server"&gt;&lt;/asp:ToolkitScriptManager&gt;
      &lt;asp:Button runat="server" ID="hiddenTargetControlForModalPopup" style="display:none"/&gt;
          &lt;asp:ModalPopupExtender runat="server" ID="programmaticModalPopup"
              BehaviorID="programmaticModalPopupBehavior"
              TargetControlID="hiddenTargetControlForModalPopup"
              PopupControlID="programmaticPopup" 
              BackgroundCssClass="modalBackground"
              DropShadow="True"
              PopupDragHandleControlID="programmaticPopupDragHandle"
              RepositionMode="RepositionOnWindowScroll" &gt;
          &lt;/asp:ModalPopupExtender&gt;
                &lt;asp:Panel runat="server" CssClass="modalPopup" ID="programmaticPopup" style="display:none;width:350px;padding:10px"&gt;
                   &lt;asp:Panel runat="Server" ID="programmaticPopupDragHandle" Style="cursor: move;background-color:#DDDDDD;border:solid 1px Gray;color:Black;text-align:center;"&gt;
                        Processing... Please Wait...
                   &lt;/asp:Panel&gt;
                       &lt;br /&gt;
                       &lt;div style="text-align:center;"&gt;&lt;img src="images/loader.gif" /&gt;&lt;/div&gt;
                &lt;/asp:Panel&gt; 
      &lt;div class="page"&gt;
          &lt;div class="header"&gt;
              &lt;div class="title"&gt;
                  &lt;h1&gt;
                      &lt;asp:Label ID="lblTitle" runat="server" Text="Home"&gt;&lt;/asp:Label&gt;&lt;br /&gt;
                  &lt;/h1&gt;
              &lt;/div&gt;
              &lt;div class="loginDisplay"&gt;
                  &lt;asp:Label ID="LoginStatus" runat="server" Text="Not logged in."&gt;&lt;/asp:Label&gt;
              &lt;/div&gt;
              &lt;div class="clear hideSkiplink"&gt;
                   &lt;asp:Menu ID="NavigationMenu" runat="server" CssClass="menu" 
                      EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"&gt;
                       &lt;DynamicMenuItemStyle CssClass="dynamic" /&gt;
                      &lt;Items&gt;
                          &lt;asp:MenuItem NavigateUrl="~/Home.aspx" Text="Home"&gt;&lt;/asp:MenuItem&gt;
                          &lt;asp:MenuItem NavigateUrl="~/PrizeListing.aspx" Text="Prize Listing"&gt;
                          &lt;/asp:MenuItem&gt;
                          &lt;asp:MenuItem Text="Administration" 
                              Value="Administration" Selectable="False"&gt;
                              &lt;asp:MenuItem Text="Users" Value="Users" NavigateUrl="~/Admin.aspx?mode=users"&gt;&lt;/asp:MenuItem&gt;
                              &lt;asp:MenuItem Text="Prizes &gt;" Value="Prizes" NavigateUrl="~/Admin.aspx?mode=prizes"&gt;
                              &lt;asp:MenuItem Text="Prizes GL Codes"  Value="Prizes GL Codes" NavigateUrl="~/PrizesGLCodes.aspx"&gt;
                              &lt;/asp:MenuItem&gt;
                              &lt;/asp:MenuItem&gt;
                              &lt;asp:MenuItem Text="Tournaments  &gt;" Value="Tournaments" NavigateUrl="~/tournaments.aspx"&gt;
                                  &lt;asp:MenuItem Text="Tournament Templates"  Value="Tournament Templates" NavigateUrl="~/TournamentTemplates.aspx"&gt;
                              &lt;/asp:MenuItem&gt;
                              &lt;/asp:MenuItem&gt;
                             &lt;asp:MenuItem Text="Winners &gt;" Value="Winners" NavigateUrl="~/Admin.aspx?mode=winners"&gt;
                               &lt;asp:MenuItem Text="Issue Bounty Prizes" NavigateUrl="~/IssueBountyPrizes.aspx"&gt;&lt;/asp:MenuItem&gt;
                               &lt;asp:MenuItem Text="Merged\Banned Players"  Value="Merged\Banned Players" NavigateUrl="~/BannedAndHiddenPlayers.aspx"&gt;
                              &lt;/asp:MenuItem&gt;
                             &lt;/asp:MenuItem&gt;
                              &lt;asp:MenuItem Text="Inventory &gt;" Value="Inventory" 
                                  NavigateUrl="~/Inventory.aspx"&gt;
                              &lt;asp:MenuItem Text="Reconciliation Notes" Value="Reconciliation Notes" 
                                  NavigateUrl="~/ReconciliationNotes.aspx"&gt;
                              &lt;/asp:MenuItem&gt;
                              &lt;/asp:MenuItem&gt;
                              &lt;asp:MenuItem NavigateUrl="~/ReceiveInventory.aspx" Text="Receive Inventory" 
                                  Value="Receive Inventory"&gt;&lt;/asp:MenuItem&gt;
                          &lt;/asp:MenuItem&gt;
                          &lt;asp:MenuItem Text="Reports" 
                              Value="Reports" Selectable="False"&gt;
                              &lt;asp:MenuItem NavigateUrl="~/ReportsAllPrizes.aspx" Text="All Prizes" 
                                  Value="All Prizes"&gt;&lt;/asp:MenuItem&gt;
                              &lt;asp:MenuItem NavigateUrl="~/ReportsPhysicalPrizes.aspx" 
                                  Text="Physical Prize Audit" Value="Physical Prize Audit"&gt;&lt;/asp:MenuItem&gt;
                              &lt;asp:MenuItem NavigateUrl="~/ReportsReconciliation.aspx" Text="Reconciliation" 
                                  Value="Reconciliation"&gt;&lt;/asp:MenuItem&gt;
                                  &lt;asp:MenuItem NavigateUrl="~/ReportsExactTargetEmailHistory.aspx" Text="ExactTarget Email History" 
      Value="ExactTargetEmailHistory"&gt;&lt;/asp:MenuItem&gt;
                          &lt;/asp:MenuItem&gt;
                          &lt;asp:MenuItem NavigateUrl="~/About.aspx" Text="About"/&gt;
                      &lt;/Items&gt;
                  &lt;/asp:Menu&gt;
              &lt;/div&gt;
          &lt;/div&gt;
          &lt;div class="main"&gt;
              &lt;asp:ContentPlaceHolder ID="MainContent" runat="server"/&gt;
          &lt;/div&gt;
          &lt;div class="clear"&gt;
          &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="footer"&gt;
  
      &lt;/div&gt;
      &lt;/form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>You'll notice that the search button causes a girdview.databind which causes the page to call window.load. However, you'll notice that the export button does not call anything to reload the page so the window.load event is not triggered and this is where I hide the popup so it stays up forever.</p>
  
  <p>This is one of the the pages.</p>
  
  <p><img src="https://i.stack.imgur.com/XHa2R.png" alt="This is one of the the pages."></p>
  
  <p>the export button does not trigger window.load so it cannot hide the modal popup
  <img src="https://i.stack.imgur.com/VNxhD.png" alt="the export button does not trigger window.load so it cannot hide the modal popup."></p>