I have finally managed to get a Popup Window to appear and close. My problem is that when I "save" the data entered in the popup, I cannot get the parent form to reload the page and show the textbox values.
Please forgive the sloppy code and all the comments. I have read dozens of examples.
I get error "Unable to get property of 'document' of undefined or null reference" and VS2017 shows this:
I am a newbie to javascript. I think it might be related to the fact that when I am typing in a <script> block, after I type 'window', several properties pop up. When I add '.opener' I see more properties, but neither 'document' nor 'location' are
listed. It is like I am missing a reference?
Thanks in advance for all the help I know I will get.
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- Added 01/10/18 as per web example -->
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/bootstrap-multiselect.js"></script>
According to your description, you want to open parent form from popup, but get that error message.
if you want to operate the current window, you could use window.opener or window.document directly in script tag, for more details, please check the following tutorial:
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
I finally got the parent page to do a PostBack! I read so many examples and heeded the advice of those that tried to help. None of that worked. Then I stumbled on the solution.
I did not understand that the Page_Load event happens BEFORE the button Click event. DOH!
I found a post that led he here:
' Do a Postback to Display Unit and Price
Protected Sub bSavePrices_Click(sender As Object, e As EventArgs) Handles bSavePrices.Click '
tUnit = eUnit1.text
tRegPrice = ePrice1.text
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("__doPostBack('','');")
sb.Append("</script>")
Member
6 Points
22 Posts
Missing Something Fundamental About Javascript and Popup Windows
Jan 10, 2018 05:36 PM|mmieher|LINK
I have finally managed to get a Popup Window to appear and close. My problem is that when I "save" the data entered in the popup, I cannot get the parent form to reload the page and show the textbox values.
Please forgive the sloppy code and all the comments. I have read dozens of examples.
I get error "Unable to get property of 'document' of undefined or null reference" and VS2017 shows this:
<input type="submit" name="ctl00$ContentPlaceHolder1$bSavePrices" value="Save" onclick="window.opener.document.getElementById('bSavePrices').click();" id="ContentPlaceHolder1_bSavePrices" style="position:absolute;left:382px;top:260px;" />
I am a newbie to javascript. I think it might be related to the fact that when I am typing in a <script> block, after I type 'window', several properties pop up. When I add '.opener' I see more properties, but neither 'document' nor 'location' are listed. It is like I am missing a reference?
Thanks in advance for all the help I know I will get.
Marc
Relevant Code from .aspx page:
...
<asp:TableRow runat="server" width="100%" VerticalAlign ="Top">
<asp:TableCell runat="server" style="position:absolute;left:540px;">
<asp:Label ID="Label2" runat="server" Text="Price*" ForeColor="#344953" style="padding-right:10px;padding-left:10px; text-align:right" ></asp:Label>
<asp:TextBox ID="eRegPrice" runat="server" forecolor="#000000" font-size="Larger" Width="65px" Wrap="False" MaxLength="7"
style="border-bottom:solid 1px;border-bottom-color:#34495e;" BorderStyle="None" BorderColor="#34495e" TabIndex="2" ToolTip="0-9 and '.' only"></asp:TextBox>
</asp:TableCell>
<asp:TableCell runat="server" style="position:absolute;left:670px;">
<asp:Label ID="Label3" runat="server" Text="Unit*" ForeColor="#344953" style="padding-right:10px;padding-left:20px;" ></asp:Label>
<asp:TextBox ID="eUnit" runat="server" forecolor="#000000" font-size="Larger" Width="90px" Wrap="False" MaxLength="10"
style="border-bottom:solid 1px;border-bottom-color:#34495e;" BorderStyle="None" BorderColor="#34495e" TabIndex="3" ToolTip="ex: Gram 1/8 Ounce"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
...
<%-- =================================== POPUP PRICING ============================================ --%>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Button ID="bPrices" runat="server" Text="Add Prices" BorderStyle="None" ForeColor="#0066CC" BackColor="White" font-bold="True" Font-Size="16px" OnClick="bPrices_Click" class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal" style="position:absolute;left:866px;top:175px;" />
<!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!-- Include all compiled plugins (below), or include individual files as needed -->
<!-- Added 01/10/18 as per web example -->
<script src="~/Scripts/bootstrap.js"></script>
<script src="~/Scripts/bootstrap-multiselect.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
<asp:UpdatePanel ID="PricesPanel" runat="server" >
<ContentTemplate>
<asp:Panel ID="PopupPanelPrices" runat="server" class="container" BackColor="White" Width="460px" Height="300px" BorderStyle="Solid" BorderColor="Green" BorderWidth="4"
Style="position: absolute; left: 550px; top: 220px;">
<!-- Modal -->
<asp:Label ID="Label12" runat="server" Text=""></asp:Label><br /> <%-- what is this for? --%>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<asp:Label ID="Label4" runat="server" Text="Manage Multiple Pricing" Font-Names="Verdana" Font-Bold="false" Font-Size="medium" ForeColor="#344953" style="position:absolute;left:155px;top:3px;" />
</div>
<div class="modal-body">
<asp:Label ID="Label14" runat="server" Text="Unit" Font-Names="Verdana" Font-Bold="false" Font-Size="medium" ForeColor="#344953"
style="position:absolute;left:240px;top:30px;" />
<asp:Label ID="Label15" runat="server" Text="Price" Font-Names="Verdana" Font-Bold="false" Font-Size="medium" ForeColor="#344953"
style="position:absolute;left:117px;top:30px;" />
<asp:Label ID="Label34" runat="server" Text="Default?" Font-Names="Verdana" Font-Bold="false" Font-Size="medium" ForeColor="#344953"
style="position:absolute;left:320px;top:30px;" />
<asp:TextBox ID="eUnit1" runat="server" forecolor="#000000" font-size="Larger" Width="90px" Wrap="False" MaxLength="10"
style="border-bottom:solid 1px;border-bottom-color:#34495e;position:absolute;left:210px;top:55px;"
BorderStyle="None" BorderColor="#34495e" TabIndex="101" ToolTip="ex: Gram 1/8 Ounce" />
<asp:TextBox ID="ePrice1" runat="server" forecolor="#000000" font-size="Larger" Width="90px" Wrap="False" MaxLength="10"
style="border-bottom:solid 1px;border-bottom-color:#34495e;position:absolute;left:90px;top:55px;"
BorderStyle="None" BorderColor="#34495e" TabIndex="100" />
<asp:RadioButton ID="rbDef1" runat="server" forecolor="#000000" font-size="Larger" TabIndex="102" GroupName="Price"
</div>
<div class="modal-footer">
<asp:Button ID="bClosePrices" runat="server" Text="Close" OnClick="bClosePrices_Click" style="position:absolute;left:25px;top:260px;" />
<asp:Button ID="bSavePrices" runat="server" Text="Save" OnClick="bSavePrices_Click"
OnClientClick="window.opener.document.getElementById('bSavePrices').click();"
style="position:absolute;left:382px;top:260px;" />
</div>
</div>
</div>
</div>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<%-- This stuff was tried. Bombed. Not currently used. --%>
<script type="text/javascript">
function close_window() {
if (window.opener && !window.opener.closed) {
window.opener.location.reload(true);
}
}
</script>
<%--
<script type="text/javascript">
function openModal() {
$('#myModal').modal('show');
}
function closeModal() {
$('#myModal').modal('hide');
}
</script>
--%>
<%-- ================================ END POPUP PRICING ============================================== --%>
CODE BEHIND: (none of the commented out stuff worked)
Protected Sub bSavePrices_Click(sender As Object, e As EventArgs) Handles bSavePrices.Click
eUnit.Text = eUnit1.text
eRegPrice.Text = ePrice1.text
' ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Pop", "closeModal();", True)
PopupPanelPrices.Style.Add("display", "none")
' does not work ClientScript.RegisterStartupScript(GetType(Page), "autoPostback", ClientScript.GetPostBackEventReference(Me, String.Empty), True)
'ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Pop", "__doPostback();", True)
'Response.Write("<script>window.opener.location.reload();</script>")
'ScriptManager.RegisterClientScriptBlock(Me, Me.GetType(), "Pop", "close_window();", True)
End Sub
Contributor
6430 Points
2693 Posts
Re: Missing Something Fundamental About Javascript and Popup Windows
Jan 11, 2018 10:52 AM|Eric Du|LINK
Hi mmieher,
According to your description, you want to open parent form from popup, but get that error message.
if you want to operate the current window, you could use window.opener or window.document directly in script tag, for more details, please check the following tutorial:
https://developer.mozilla.org/en-US/docs/Web/API/Window
In your case, you want to operate parent form from popup, please check the following tutorials:
https://www.ibm.com/developerworks/community/blogs/hazem/entry/accessing_parent_window_elements_from_child_window_in_javascript5?lang=en
https://stackoverflow.com/questions/16300117/submit-form-in-parent-window-from-popup
Best Regards,
Eric Du
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
6 Points
22 Posts
Re: Missing Something Fundamental About Javascript and Popup Windows
Jan 11, 2018 03:00 PM|mmieher|LINK
I finally got the parent page to do a PostBack! I read so many examples and heeded the advice of those that tried to help. None of that worked. Then I stumbled on the solution.
I did not understand that the Page_Load event happens BEFORE the button Click event. DOH!
I found a post that led he here:
' Do a Postback to Display Unit and Price
Protected Sub bSavePrices_Click(sender As Object, e As EventArgs) Handles bSavePrices.Click '
tUnit = eUnit1.text
tRegPrice = ePrice1.text
Dim sb As System.Text.StringBuilder = New System.Text.StringBuilder()
sb.Append("<script language='javascript'>")
sb.Append("__doPostBack('','');")
sb.Append("</script>")
ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "JSCR", sb.ToString(), False)
end sub
There is NOTHING special in the aspx file other than the bSavePrices button.
Hope this helps somebody else.
Marc