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)
{
}
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. |
|
|
 |
|
 |
Thanks for the suggestions. I don't know why it's not working in firefox. I will take a look into that issue...
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
I had checked your source code and it is right.but i don't know why it is not working in Firefox..
So it is good option to use a button for this. button click on parent page and call it from child page ...
on parent page aspx <input id="htmlbutton" type="button" runat="server" value="html button" önserverclick="htmlbutton_ServerClick" /> on parent page .cs protected void htmlbutton_ServerClick(object sender, EventArgs e) { lblMessage.Text = "From child html button"; }
from popup window call this
string code = "window.opener.document.getElementById('htmlbutton').click();";
if (!ClientScript.IsClientScriptBlockRegistered("someKey")) { ClientScript.RegisterClientScriptBlock(this.GetType(), "someKey", code, true); } you can even use asp button on parent page..
Regards Ulfat Hussain,Pakistan.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
|
 |
|
 |
please explain , I cannot understand this sentence. "Ensure that u hide the button the client side and not on the server."
Regards
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
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
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
Are you saying about the button which is on the parent form. If we have to hide this so that user cannot see this button to prevent user to click it then we can put it in a div.
If we set its visible to false then it cannot be accessed.I had tested it using this so that it is not visible to user but we can get its instance and its instance is an object.
place a div in aspx form.set its visibility hidden.display : none.. then place button tag then close div
Regards Ulfat Hussain,Pakistan.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
 |
That exactly what I'm trying to say! But instead of putting the button in a div tag, just set the display : none on the button itself rather than on the div. Doing so you are achieving the same with one less tag. Might not make big difference in all cases, but would definitely help when having complex pages with multiple such hidden buttons.
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
 |
Yes.you are right.Setting display:none is a better approach for this.Now I will use this onward for such html display.Thanks.
|
| Sign In·View Thread·PermaLink | |
|
|
|
 |
|
|
 |
|
 |
Hi Dave
Nice article, just though I might let you know that the code does not work in Firefox. When I check the error console after clicking the call parent button I get the following error.
Error: window.opener.document.getElementById("popUpAnchor").click is not a function Source File: http:Line: 24
|
| Sign In·View Thread·PermaLink | 5.00/5 |
|
|
|
 |
|
|
 |
|
|