RSS

Use Popup To Postback/Update It’s Parent/Opener Without losing ViewState Values And Close

12 Jun


Summery:

Solution for a very common problem about communication between Asp.net page to popups.
Suitable for every .Net framework especially for .Net 1 .

Scenarios:

  1. You have a button that performs an action which  requires custom authentication. You’d want to intercept the flow of the button Click event, raise a popup and request a password, for example, and then continue the button flow.
  2. You have an advanced form with popups involved (requiring additional information).
    You’d like open a popup, let the user make action and return to the opener form with altered data while keeping the entered data.

The Problem:

Approaching this problem raised a question: how to inject a code which could make it possible to get out of an event flow, wait for another user action, and then resume the event on the source code.
Or for example, how do you refresh the parent page and resume to the Click event on the submit button.

 The common way is to open a popup, and on submit do some Server-Side, close the popup and refresh the opener. This enforces the code to mess up the OnPage_Load event to catch the wanted request, and make loss of ViewState data (because it a fresh new request).

The Solution:

  1. Use GetPostBackEventReference (“__doPostBack” javascript function)  with the source control (button) as parameter.
  2. Use Session to store an indicator if to continue or to intercept again.
  3. Use a Javascript callback function on the parent page that will contain the __doPostBack call with the source button parameter.

Review the following code as example to the suggested:

The Code:

// On the button control, inside the event
// Check if indicating to continue or intercept, open the popup,
// and define the callback with the "sender" (the button itself)
if (!(bool)Session["IsAllowed"])
{
	// Open popup to enter the password, and define a callback
	Page.RegisterStartupScript(
		"openwindow", "<script>window.open('http://myurl.aspx'" +
		",'title');" +
		"myPage_Callback = function(){" +
		Page.GetPostBackEventReference((System.Web.UI.Control)sender,"") +
		"};" +
		"</script>"
	);

	return;
}
// On popup window, on event of the submit button 
// Calls the __doPostBack callback js function on the opener and closes the popup.
Session["IsAllowed"] = true;
Page.RegisterStartupScript(
	"closewindow", "<script>window.opener.window.myPage_Callback();" +
	"window.close();" +
	"</script>"
);
Advertisements
 

Tags: , , , , ,

2 responses to “Use Popup To Postback/Update It’s Parent/Opener Without losing ViewState Values And Close

  1. Johne399

    July 30, 2014 at 9:56 pm

    This is really interesting, You’re a very skilled blogger. I have joined your feed and look forward to seeking more of your magnificent post. Also, I have shared your web site in my social networks! efdfcddcdkgc

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: