Click here to Skip to main content
15,885,856 members
Articles / Web Development / HTML
Article

Pop-up Window Calling Parent Window's Server-side Code

Rate me:
Please Sign up or sign in to vote.
3.82/5 (9 votes)
24 Oct 2008CPOL1 min read 64.2K   667   22   12
Pop-up window calling parent window's server-side code in ASP.NET.

Introduction

When a parent window pop-ups a child window, there's no direct way for the child to call the parent's server-side code. This article presents a workaround for the child pop-up window to call the parent's server side code, i.e., the code-behind

Background

I have been trying to find a way for the child window to call the parent's server-side code. After some Googling, I couldn't find any. So, I came up with a workaround with some JavaScript code. I am not sure if this workaround will impose any security issues; any feedback is greatly appreciated.

Using the code

The idea is to declare an Anchor tag with an onClick function in the parent window. Then, we will have the child window invoke the Click event with some JavaScript.

First, declare an Anchor tag in the parent window with runat="server".

HTML
<a id="anchorId" runat="server" onclick="return true" onserverclick="foo"></a> 

Onserverclick is the server-side function that will be called if onclick returns true. The child will invoke the Click event on this anchor, which always returns true, so the server-side function can be invoked.

In the parent's code-behind, we include the event-handler for this function:

C#
protected void foo(object sender, EventArgs e)
{
    // Do something here
}

In the child window, we can then use JavaScript to invoke the Click event:

C#
string code = "<script> " + 
  "window.opener.document.getElementById('popUpAnchor').click();</script>";

if (!ClientScript.IsStartupScriptRegistered("someKey"))
{
    ClientScript.RegisterStartupScript(this.GetType(), "someKey", code);
}

If you are using a master page and wrapping the anchor tag with <contentPlaceHolder>, you will need to figure out the ID for the anchor by viewing the HTML source.

Here's the code to pop-up a window:

C#
string code = "<script>window.open('popupWindow.aspx',null,'left=400," + 
       " top=100,height=400, width=400, status=no, resizable= no, scrollbars= no," + 
       "toolbar= no,location= no, menubar= no');</script>";

if (!ClientScript.IsStartupScriptRegistered("someKey"))
{
    ClientScript.RegisterStartupScript(this.GetType(), "someKey", code);
}

Hope this helps!

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Canada Canada
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
Questionhello Pin
AyeNaingKye29-Oct-12 5:18
AyeNaingKye29-Oct-12 5:18 
GeneralThanks for the suggestion Pin
Dave2342344228-Oct-08 3:43
Dave2342344228-Oct-08 3:43 
GeneralSuggestion Pin
ulfat hussain27-Oct-08 19:23
ulfat hussain27-Oct-08 19:23 
GeneralRe: Suggestion Pin
Dinesh Mani28-Oct-08 0:54
Dinesh Mani28-Oct-08 0:54 
GeneralRe: Suggestion Pin
ulfat hussain28-Oct-08 17:09
ulfat hussain28-Oct-08 17:09 
GeneralRe: Suggestion Pin
Dinesh Mani28-Oct-08 17:32
Dinesh Mani28-Oct-08 17:32 
Apologies on the typo! It should have been
"Ensure that you hide the button on the client side and not on the server side."

HTH
GeneralRe: Suggestion Pin
ulfat hussain28-Oct-08 18:06
ulfat hussain28-Oct-08 18:06 
GeneralRe: Suggestion Pin
Dinesh Mani28-Oct-08 19:06
Dinesh Mani28-Oct-08 19:06 
GeneralRe: Suggestion Pin
ulfat hussain29-Oct-08 19:40
ulfat hussain29-Oct-08 19:40 
GeneralRe: Suggestion Pin
Dave2342344229-Oct-08 4:55
Dave2342344229-Oct-08 4:55 
GeneralNot working in Firefox Pin
Syed M Hussain26-Oct-08 2:14
Syed M Hussain26-Oct-08 2:14 
GeneralRe: Not working in Firefox Pin
Dave2342344229-Oct-08 5:43
Dave2342344229-Oct-08 5:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.