"
ASP.NET (snapshot 2017) Microsoft documentation and samples

Launching a Modal Popup Window from Server Code (VB)

by Christian Wenz

Download Code or Download PDF

The ModalPopup control in the AJAX Control Toolkit offers a simple way to create a modal popup using client-side means. However some scenarios require that the opening of the modal popup is triggered on the server-side.

Overview

The ModalPopup control in the AJAX Control Toolkit offers a simple way to create a modal popup using client-side means. However some scenarios require that the opening of the modal popup is triggered on the server-side.

Steps

First of all, an ASP.NET Button web control is required to demonstrate how the ModalPopup control works. Add such a button within the <form> element on a new page:

[!code-aspxMain]

   1:  <asp:Button ID="ClientButton" runat="server" Text="Launch
   2:   Modal Popup (Client)" />

Then, you need the markup for the popup you want to create. Define it as an <asp:Panel> control and make sure that it includes a Button control. The ModalPopup control offers the functionality to make such a button close the popup; otherwise there is no easy way to let it vanish.

[!code-aspxMain]

   1:  <asp:Panel ID="ModalPanel" runat="server" Width="500px">
   2:   ASP.NET AJAX is a free framework for quickly creating a new generation of more efficient,
   3:   more interactive and highly-personalized Web experiences that work across all the
   4:   most popular browsers.<br />
   5:   <asp:Button ID="OKButton" runat="server" Text="Close" />
   6:  </asp:Panel>

Next add the ModalPopup control from the ASP.NET AJAX Toolkit to the page. Set properties for the button which loads the control, the button which makes it disappear, and the ID of the actual popup.

[!code-aspxMain]

   1:  <ajaxToolkit:ModalPopupExtender ID="mpe" runat="server"
   2:   TargetControlId="ClientButton" PopupControlID="ModalPanel" 
   3:   OkControlID="OKButton" />

As with all web pages based on ASP.NET AJAX; the Script Manager is required to load the necessary JavaScript libraries for the different target browsers:

[!code-aspxMain]

   1:  <asp:ScriptManager ID="asm" runat="server" />

Run the example in the browser. When you click on the button, the modal popup appears. In order to achieve the same effect using server-side code, a new button is required:

[!code-aspxMain]

   1:  <asp:Button ID="ServerButton" runat="server" Text="Launch Modal Popup (Server)" 
   2:   OnClick="ServerButton_Click" />

As you can see, a click on the button generates a postback and executes the ServerButton_Click() method on the server. In this method, a JavaScript function called launchModal() is executed to be exact, the JavaScript function will be executed once the page has been loaded:

[!code-aspxMain]

   1:  <script runat="server">
   2:   Sub ServerButton_Click(ByVal sender As Object, ByVal e As EventArgs)
   3:   ClientScript.RegisterStartupScript(Me.GetType(), "key", "launchModal();", True)
   4:   End Sub
   5:  </script>

The job of launchModal() is to display the ModalPopup. The launchModal() function is executed once the complete HTML page has been loaded. At that moment, however, the ASP.NET AJAX framework has not been fully loaded yet. Therefore, the launchModal() function just sets a variable that the ModalPopup control must be shown later on:

[!code-htmlMain]

   1:  <script type="text/javascript">
   2:   var launch = false;
   3:   function launchModal() 
   4:   {
   5:   launch = true;
   6:   }

The pageLoad() JavaScript function is a special function that gets executed once ASP.NET AJAX has been fully loaded. Therefore we add code to this function to show the ModalPopup control, but only if launchModal() has been called before:

[!code-javascriptMain]

   1:  function pageLoad() 
   2:   {
   3:   if (launch) 
   4:   {
   5:   $find("mpe").show();
   6:   }
   7:   }
   8:  </script>

The $find() function is looking for a named element on the page and expects the server-side ID as a parameter. Therefore, $find("mpe") returns the client representation of the ModalPopup control; its show() method lets the popup appear.

The modal popup appears when either of the buttons is clicked

The modal popup appears when either of the buttons is clicked (Click to view full-size image)

Previous Next



Comments ( )
Link to this page: //www.vb-net.com/AspNet-DocAndSamples-2017/aspnet/web-forms/overview/ajax-control-toolkit/modalpopup/launching-a-modal-popup-window-from-server-code-vb.htm
< THANKS ME>