Make postback when the OkButton in a ModalPopup is clicked in ASP.NET AJAX

segaco picture segaco · Apr 30, 2009 · Viewed 37k times · Source

I follow the example in Atlas: Creating a Confirmation Using the ModalPopup Extender to make a postback when the OkButton in a ModalPopup is clicked (it uses the ModalPopupExtender that comes in ASP.NET Ajax Control Toolkit), but as I can see, the Sys.WebForms.PostBackAction() is no longer present in ASP.NET AJAX (the example is for Atlas). When I run it the "Microsoft JScript runtime error: Object expected" error message raises, in the line in javascript where I create the PostBackAction. How can I make this works in ASP.NET AJAX, or maybe, there's another way to do it? Thanks

Answer

Tim Scarborough picture Tim Scarborough · May 22, 2009

Server side events do work with the ModalPopupExtender. What you need to do is to create a button with a display style set to none. Then set the TargetControlID property of the ModalPopupExtender to the hidden button ID.

<asp:Button ID="btnShowModal" runat="server" Text="Show" OnClick="btnShowModal_Click" />
<asp:Button ID="btnHidden" runat="server" Style="display: none" />
<ajaxControlToolkit:ModalPopupExtender ID="modalExtender" runat="server" 
    TargetControlID="btnHidden" PopupControlID="divId" />

In the OnClick handler show the modal:

modalExtender.Show();

In the handler for the control that will close the modal (usually a button) hide the modal:

modalExtender.Hide();

I'm able to successfully use this method for posting back to the server in order to populate controls in my modal with info from a database during the postback. Works like a champ.