Welcome to AspAdvice Sign in | Join | Help

ASP.NET: How to create a postback on a main page from a popup window

EDIT: Updated for ASP.NET 2.0 

First: you have main.aspx which is the main page having the stuff you want to attach to a postback caused from a popup, for example filtering or updating a DataGrid etc etc. In this example however, there is a Label which we set to point the current time when a postback happens. Then there's a dummy button which works as "actor" for the postback. It is non-visible because we want it to be used only from the popup, though it could also be visible. And finally, there's of course a client-side button to open the popup.

Next I show the relevant parts of the pages.

main.aspx

<input type=button onclick="openPopUp()" value="Open the popup" />
<asp:Button ID="btnPostback" runat="server" Visible="false" OnClick="btnPostBack_Click" />
<asp:Label ID="lblShowPostInfo" runat=server />

<script language=javascript>
//To cause postback "as" the Button
function PostBackOnMainPage(){
  <%=GetPostBackScript()%>
}


//Helper just to open popup
function openPopUp(){
window.open('popup.aspx','popup','width=400,height=100');
//Maybe handling something else also, like giving extra arguments etc etc
}

</script>


<script runat="server" language="VB" >
    'Create the postback script
    Private Function GetPostBackScript() As String
       
        Dim options As New PostBackOptions(btnPostback)
        Page.ClientScript.RegisterForEventValidation(options)
        Return Page.ClientScript.GetPostBackEventReference(options)
       
    End Function
   
   
'This is to react to the postback
Protected Sub btnPostBack_Click(ByVal sender As Object, ByVal e As EventArgs)
   lblShowPostInfo.Text = "Postback happened: " & DateTime.Now.ToString()
End Sub
</script>

And then we have the popup, popup.aspx, which causes the postback and is opened from main.aspx. It could have some sort of  process to control the main page. Here's there's just a dummy button to cause the postback on main page

popup.aspx

<input type=button onclick="opener.PostBackOnMainPage()" value="Cause a postback on opening page" />

Idea is simply to create a javascript call that acts as if the hidden server-side button control on the main.aspx would have been clicked. And this javascript can be called from the popup when popup window has reference to its opener via opener property (window.opener). So when opener.PostBackOnMainPage ()  is called in the popup, it calls the method PostBackOnMainPage on the main page which again causes the postback as if the Button would have been clicked.

Simple, isn't it? :-)

Published Wednesday, June 15, 2005 11:41 PM by joteke
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: ASP.NET: How to create a postback on a main page from a popup window

Very handy, though I'd strongly recommend against using popups for anything, in this world of popup blockers and non-IE browsers.
Thursday, June 16, 2005 2:45 AM by joteke

# re: ASP.NET: How to create a postback on a main page from a popup window

How the code suppose to work???
1)Server-side button with Visible=false attribute wont be rendered on client
2)There is no code in PostBackOnMainPage function
Thursday, June 16, 2005 11:51 AM by joteke

# re: ASP.NET: How to create a postback on a main page from a popup window

Anatoly, the Button does not have to be visible, e.g it does not have to be rendered.

The call to <%=Page.GetPostBackEventReference(btnPostBack)%> (not sure if colors in the post make it show a bit dimmed) inside PostBackOnMainPage will create the __doPostBack call into it (renders it by the time page is rendered), and that points the Button (which isn't visible).

We are interested in getting that __doPostBack called from the popup and this __doPostBack call is located is inside PostBackOnMainPage method.
Thursday, June 16, 2005 10:17 PM by joteke

# re: ASP.NET: How to create a postback on a main page from a popup window

I now see what you mean.
Thanks.
Friday, June 17, 2005 9:18 AM by joteke

# Initiate a Postback on Main Page from Popup Window

Wednesday, June 15, 2005 4:56 PM by TrackBack

# re: ASP.NET: How to create a postback on a main page from a popup window

Hi! This did the trick for me! Thanks!
Wednesday, December 21, 2005 5:26 AM by Aimee

# Nice trick

This was a nice trick, it helped me a in solving a problem that was lingering since a long time as a user annoyance.

thanks and all the best.
Wednesday, May 24, 2006 7:57 PM by tiy

# re: ASP.NET: How to create a postback on a main page from a popup window

Thank you very much for this post, it helps me a lot!
Thursday, August 03, 2006 7:37 AM by Thomas

# ASP.NET: causing a postback after modal dialog is closed

This one was asked on email lists. Here is a sample. It also demonstrates how to combine event validation...
Saturday, August 05, 2006 4:22 AM by Joteke's Blog

# re: ASP.NET: How to create a postback on a main page from a popup window

thnx for the trick, really help me out, and it's easy !!!
Tuesday, August 15, 2006 4:06 PM by jOE

# re: ASP.NET: How to create a postback on a main page from a popup window

f'ing awesome. Exactly what I needed. Thx
Tuesday, February 24, 2009 7:42 PM by mmadink

# re: ASP.NET: How to create a postback on a main page from a popup window

Hi, Thanks for the article. I found it very useful. Can you please tell how I can pass argument from the GetPostBackScript() function? Thanks in advance.
Wednesday, March 25, 2009 2:18 AM by Rashmi

# How to stop postback on a main page from a model popup window

Hi i have an issue like unnecessarily parent window is getting postback but i don't want Scenario :when i click image button, change password model popup window will display i will do changes and close then it shouldn't postback parent window.
Tuesday, April 07, 2009 9:33 AM by sudheer

# re: ASP.NET: How to create a postback on a main page from a popup window

This is Great!! Helped me a lot!!! Is it at all possible to cause a postback on a popup from the main window??
Wednesday, May 20, 2009 5:32 AM by Shane

# re: ASP.NET: How to create a postback on a main page from a popup window

This is really awesome !! I was looking it for years. All other colleagues appreciated. Thanks a lot.
Thursday, June 17, 2010 5:24 PM by Nirav Vyas

# re: ASP.NET: How to create a postback on a main page from a popup window

Great tutorial. this really helped me to figure out how popup windows post backs. thanks a lot
Thursday, November 11, 2010 5:07 AM by Salih Bal

Leave a Comment

(required) 
required 
(required) 
Enter the code you see below