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