ASP.NET, JavaScript
Working on my recent project, i ran into a common problem that most developers have had when they are dealing with user input and page refresh. The requirement is the webpage needs a way to popup a window, let the user input the data. After the form is submitted, the popup is closed, and the original webpage is refreshed with new data already filled in. The solution is simple, but it took me awhile to find out the best answer that fits what i want.
I just point out the important thing of how i approached this problem rather than going into detail the code. The major things lay on how we refresh the original window (also called the opener in Javascript) and how we fill the form with user input.
Session variables are the the answer for the 2nd problem. Simply store all user inputs into session variables, and when the original page (opener) refreshes, make sure you check these session variables and fill out the fields appropriately if those sessions are not empty (but make sure you “empty” those session as well after you fill in the values or after the main form is processed unless you have to invoke the popup multiple times).
To refresh the original page (opener), insert this script to wherever you may want to invoke it (e.g: a button, a link):
<script language=’javascript’>
{ opener.window.location=opener.window.location;self.close();}
</script>
In VB.NET, you can add to the event handler of a button as:
Response.Write(”<script language=’javascript’> {opener.window.location=opener.window.location;self.close();}</script>”)
Yup, it’s just simple like that. The reason i bring this up because i want it to be my reference later on since there are some helps out there if you Google, but most of them don’t fit what i want. For instance, you can insert a meta tag to refresh the page, but you have to specify the time delay so that the page can refresh after certain intervals (I just want the page to be refreshed when I want only). Or the other workaround is to declare a javascript function in the original window as: document.location.reload()and call this function when the form in the popup window is submitted. This one works fine except for the annoying popup msg asking if you want to send data to the server (of course we want the server to get the session variables to process).
Note: I haven’t tested this on Firefox, Netscape or other browsers for the compatibility