`

如何在弹出的窗口中引发父窗口的postback事件

阅读更多

看到一篇老外的介绍,介绍如何在弹出的窗口中引发父窗口的postback事件,现转载如下:
首先,建立main.aspx文件,写入如下代码,其中的一个按钮设置为visiable,只是为了效果而设置。
<script language="javascript">
//To cause postback "as" the Button
function PostBackOnMainPage(){
  <%=Page.GetPostBackEventReference(btnPostBack)%>
}
//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">
'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>
            <form id="Form1" method="post" runat="server">
                  <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" />
            </form>
可以看到,button按钮引发openpopup的事件,弹出窗口。为了明显,设置一个文本lblshowpostinfo,显示引发postback的时间.而在popup.aspx中,代码如下:
 <input type=button onclick="opener.PostBackOnMainPage()" value="Cause a postback on opening page" />

作用是点这个按钮,引发父窗口的postbackonmainpage()事件

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics