Disclaimer The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.
Ever need to open a modal dialog in a web application? Unless you've only written the simplest of applications, then the answer is a resounding "YES!" There is certainly more than one way to accomplish this. But this particular answer suited my needs perfectly.
What I needed: Display a list of items for the user to choose in a modal window. If the item they require is not available, then allow them to add the item, with extra details. In this case the item represents a company, and the extra details are the company address, phone numbers, fax, notes, etc. Sounds easy enough.
There were two problems, though.
Modal Form Opens a Blank WindowClosing the window causes another - blank - window to open in it's place. Weird but easily solvable. Simply place this code in the <HEAD> section of the modal form:<base target="_self">Now the the blank window is no longer opened. The Modal Window is Cached!The content of the window is cached on the server. So when it is opened for the second time, the Page_Load event is not fired. The problem for me was that the contents of the combo box were not refilled, so any new companies that were added manually weren't reflected in the list! I saw a multitude of answers while searching, but this one seemed to make more sense to me than the others. In the declarations section of the form (very top of the HTML code) enter the following line:<%@ OutputCache Location="None" VaryByParam="None" %> I should mention that I did find numerous other answers to the caching problem that I did not try. If the above answer doesn't fit your situation, you might try some of these: add this to the modal window's page_load: Response.Expires = 0; Response.Cache.SetNoStore(); Response.AppendHeader("Pragma", "no-cache"); Or this:Response.Cache.SetNoStore(); Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-500)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.AppendHeader("Pragma", "no-cache"); Or any combination of the above. I did not require any code in the page_load method.
Modal Form Opens a Blank WindowClosing the window causes another - blank - window to open in it's place. Weird but easily solvable. Simply place this code in the <HEAD> section of the modal form:<base target="_self">Now the the blank window is no longer opened.
The Modal Window is Cached!The content of the window is cached on the server. So when it is opened for the second time, the Page_Load event is not fired. The problem for me was that the contents of the combo box were not refilled, so any new companies that were added manually weren't reflected in the list! I saw a multitude of answers while searching, but this one seemed to make more sense to me than the others. In the declarations section of the form (very top of the HTML code) enter the following line:<%@ OutputCache Location="None" VaryByParam="None" %>
I should mention that I did find numerous other answers to the caching problem that I did not try. If the above answer doesn't fit your situation, you might try some of these:
add this to the modal window's page_load: Response.Expires = 0; Response.Cache.SetNoStore(); Response.AppendHeader("Pragma", "no-cache");
Or this:Response.Cache.SetNoStore(); Response.Cache.SetExpires(DateTime.Now.AddMilliseconds(-500)); Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.AppendHeader("Pragma", "no-cache");
Or any combination of the above. I did not require any code in the page_load method.
Remember Me