Click here to Skip to main content
Licence CPOL
First Posted 24 Oct 2008
Views 24,777
Downloads 170
Bookmarked 21 times

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

By Dave23423442 | 24 Oct 2008
Pop-up window calling parent window's server-side code in ASP.NET.

1
1 vote, 11.1%
2
2 votes, 22.2%
3
1 vote, 11.1%
4
5 votes, 55.6%
5
3.82/5 - 9 votes
μ 3.82, σa 2.04 [?]

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".

<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:

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

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

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:

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)

About the Author

Dave23423442

Software Developer

Canada Canada

Member


Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks for the suggestion PinmemberDave234234424:43 28 Oct '08  
GeneralSuggestion Pinmemberulfat hussain20:23 27 Oct '08  
GeneralRe: Suggestion PinmemberDinesh Mani1:54 28 Oct '08  
GeneralRe: Suggestion Pinmemberulfat hussain18:09 28 Oct '08  
GeneralRe: Suggestion PinmemberDinesh Mani18:32 28 Oct '08  
GeneralRe: Suggestion Pinmemberulfat hussain19:06 28 Oct '08  
GeneralRe: Suggestion PinmemberDinesh Mani20:06 28 Oct '08  
GeneralRe: Suggestion Pinmemberulfat hussain20:40 29 Oct '08  
GeneralRe: Suggestion PinmemberDave234234425:55 29 Oct '08  
GeneralNot working in Firefox PinmemberSyed M Hussain3:14 26 Oct '08  
GeneralRe: Not working in Firefox PinmemberDave234234426:43 29 Oct '08  
Hey Syed. It works on my firefox fine. Maybe you can try to replace with an <input type="button" ...="" /> (see the post above) and see if that works for you.

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

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120210.1 | Last Updated 24 Oct 2008
Article Copyright 2008 by Dave23423442
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid