![]() |
Web Development »
ASP.NET »
Howto
License: The Code Project Open License (CPOL)
Pop-up Window Calling Parent Window's Server-side CodeBy Dave23423442Pop-up window calling parent window's server-side code in ASP.NET. |
C#, Javascript, HTML, .NET (.NET 2.0, .NET 3.0, .NET 3.5), ASP.NET, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
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
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.
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!
| You must Sign In to use this message board. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 24 Oct 2008 Editor: Smitha Vijayan |
Copyright 2008 by Dave23423442 Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |