I always wanted to open a popup in an asp .net page from the server side code, without having do a bunch of javascript hacks. This is very important in many situations; for example, you have a page where a user fills in a bunch of data and hits submit, you want to validate all these fields and if everything is good, you want to display a printable summary sheet in a popup. I work on patient/clinical systems and on many occasions, we are required to allow users to print professional looking forms so that they can file them in the patient charts. There is no way we can effectively design a page to be interactive and simple and at the same time be printable.
There are several options for doing popups in asp.net
- The most popular and simplest option is of course to call the javascript window.open function on a button – in asp.net the standard practice is to register the window.open on the page load
btnSubmit.Attributes [“onClick”] = “javascript:window.open (‘http://someurl.com’);}
public void Page_Load{if (!Page.IsPostBack)
The biggest problem with this is that you have to know the url beforehand, if you want the user to enter some data before he clicks the button, you cannot do server side validations before allowing the popup. - Another simple, but often overlooked way to initiate a popup from the server side is to register a popup url so that once the postback is complete, it can be executed on the client side. Here’s some simple steps to get this working….a. Create a hidden field on the page<input type=”hidden” id=”hdnPopupUrl” value=”” />b. Create a body onLoad handler function that reads the hidden variable and does a window.openadd the onload to your page body…..
function myLoad () {
if (document.getElementById ('hdnPopupUrl').value != '')
window.open (document.getElementById ('hdnPopupUrl').value);
document.getElementById ('hdnPopupUrl').value = '';
} - After moving from regular to asp.net to ajax asp.net, i realized that i couldn’t continue doing my popups because of partial postbacks. One of the choices I made when moving to ajax was to wrap my entire content (minus the theme, skin, header, side menus, etc) in an ajax updatepanel – although this is not considered to be a good option, i found that it simplifies the ajaxification of my project and yet allows me to switch with ease to a non-ajax version by simple disabling the updatepanel. As for popups, they don’t work anymore because the ‘onLoad’ function is no longer called on a partial postback. After much googling and searching the microsoft ajax reference, I realized that the solution was simpler than I originally thought…..a. On the server-side, register a new hidden label field and populate it with popup url using the registerdataitem function
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);Label lblPopupUrl = new Label();
lblPopupUrl.ID = "myPopupUrl";
lblPopupUrl.Visible = false;
scriptManager.RegisterDataItem(lblPopupUrl, popupUrl);
b. On the client side, handle the EndRequest event of the ajax PageRequestManager//Set the handlers for start and end of async postback
var prm = Sys.WebForms.PageRequestManager.getInstance();
prm.add_initializeRequest(InitializeRequest);
prm.add_endRequest(EndRequest);
function EndRequest(sender, args)
{var dataItems = args.get_dataItems();
//alert (dataItems["myPopupUrl"]);if (dataItems["myPopupUrl"] != null) {
window.open (dataItems["myPopupUrl"], 'NewWindow');
}
} - Finally, the Ajax Control Toolkit (http://ajax.asp.net/ajaxtoolkit/) has a extender called ‘ModalPopup’. Now the only problem with this control is that your popup is required to be a control within the page on which ModalPopup is called. Although I can’t do a true popup with this control, I thought it required a mention because it is a great way to do smaller popups within say a datagrid or a list where you want to do a minimal ‘New’ or ‘Edit’ windows without having to write separate pages.