locked
Pop up custom calculator child window with few values RRS feed

  • Question

  • User-1519490041 posted

    Hi,

    I have done a window design code as below and it is a CHILD window that trigger by a button at PARENT form. So now, i am able to return 1 value and set to the parent's text box. How if i wan to pass more value back to the parent text field?  any suggestion?

    Here the code for parent window

     protected void Page_Load(object sender, EventArgs e)
            {
                string cal = @"PopUp('Calculator.aspx?ctrl=ctl00_PlaceHolderMain_txtProposeAir')";
    
                this.btnCal.Attributes.Add("onclick", cal);
    }

    Here the code for Child window

    <script type="text/javascript">
    
            function close()
            {
                window.close();
            }
    
            function DHLupdateParent()
            {
                var total= document.getElementById('<%= txtDHLTotal.ClientID%>').value;
                window.opener.document.getElementById('<%= Request["ctrl"] %>').value = total;
                //return total;
                //window.close();
            }
    
            function TNTupdateParent() {
                var total = document.getElementById('<%= txtTNTTotal.ClientID%>').value;
                window.opener.document.getElementById('<%= Request["ctrl"] %>').value = total;
                //return total;
               // window.close();
            }
    
            function TNTTotal(fuleSurchage)
            {
                document.getElementById('<%= txtTNTTotal.ClientID%>').value = fuleSurchage + parseFloat(document.getElementById('<%= txtTNTRate.ClientID%>').value) + parseFloat(document.getElementById('<%= txtTNTSecurityCharge.ClientID%>').value);
            }
    
            function tntfuleSurchage(tntSecurityCharge)
            {
                var securityCharge = parseFloat(document.getElementById('<%= txtTNTSecurityCharge.ClientID%>').value);
                var importCharge = parseFloat(document.getElementById('<%= txtTNTImportCharge.ClientID%>').value);
                var Rate = parseFloat(document.getElementById('<%= txtTNTRate.ClientID%>').value);
                var fuleSurchageRate = parseFloat(document.getElementById('<%= txtTNTFuleRate.ClientID%>').value);
    
                var fuleSurchage = (securityCharge + importCharge + Rate) * fuleSurchageRate / 100;
                document.getElementById('<%= txtTNTFuleCharge.ClientID%>').value = fuleSurchage;
                TNTTotal(fuleSurchage);
    
            }
    
            function tntSecurityCharge()
            {
                var weight = parseFloat(document.getElementById('<%= txtTNTWeight.ClientID%>').value);
                var rate = parseFloat(document.getElementById('<%= txtTNTSecurityRate.ClientID%>').value);
    
                document.getElementById('<%= txtTNTSecurityCharge.ClientID%>').value = rate * weight;
                tntfuleSurchage(rate * weight);
            }
    
            function ComputeCosts(dhlfuleSurchage)
            {
                var dhlrate = parseFloat(document.getElementById('<%= txtDHLRate.ClientID%>').value);
    
                document.getElementById('<%= txtDHLTotal.ClientID%>').value = dhlrate + dhlfuleSurchage;
            }
    
            function dhlfuleSurchage()
            {
                var fuleRate = parseFloat(document.getElementById('<%= txtDHLFuleRate.ClientID%>').value);
                var dhlrate = parseFloat(document.getElementById('<%= txtDHLRate.ClientID%>').value);
    
                var surcharge = parseFloat(document.getElementById('<%= txtDHLFule.ClientID%>').value = fuleRate * dhlrate / 100);
                ComputeCosts(surcharge);
            }
    
        </script>
    
    
    <form id="form1" runat="server">
            <div>
                <fieldset>
                    <legend>DHL</legend>
                    <table width="100%">
                         <tr><td>Weight (KG)</td><td><asp:TextBox runat="server" ID="txtDHLWeight" Text="0.00"/></td></tr>
                        <tr><td>Rate</td><td><asp:TextBox runat="server" onkeyup="ComputeCosts();" Text="0.00" ID="txtDHLRate" /></td></tr>
                        <tr><td>Fule Surcharges</td><td><asp:TextBox runat="server" onkeyup="dhlfuleSurchage();" Text ="0.00" Enabled="false" ID="txtDHLFule" /></td><td>Current Fule Rate</td><td><asp:TextBox runat="server" onkeyup="dhlfuleSurchage();" Text="0.00" ID="txtDHLFuleRate" /></td></tr>
                        <tr><td>Total (RM)</td><td><asp:TextBox runat="server" ID="txtDHLTotal" Text="0.00" /></td></tr>
                         
                        <tr><td><asp:Button ID="Proposed" runat="server" Text="Button" OnClientClick="javascript:DHLupdateParent();" /></td></tr>
                    </table>
                </fieldset>
                     
                <fieldset>
                    <legend>TNT</legend>
                    <table width="100%">
                        <tr><td>Weight (KG)</td><td><asp:TextBox runat="server" ID="txtTNTWeight" Text="0.00" onkeyup="tntSecurityCharge();"/></td></tr>
                        <tr><td>Rate</td><td><asp:TextBox runat="server" ID="txtTNTRate" Text="0.00" /></td></tr>
                        <tr><td>Import Charges</td><td><asp:TextBox runat="server" Text="0.00" ID="txtTNTImportCharge" /></td></tr>
                        <tr><td>Security Charges</td><td><asp:TextBox runat="server" Text="0.00" ID="txtTNTSecurityCharge" onkeyup="tntSecurityCharge();" /></td><td>Charge Rate/KG</td><td><asp:TextBox runat="server" ID="txtTNTSecurityRate" Text="0.21" /></td></tr>
                        <tr><td>Fuel Surcharges</td><td><asp:TextBox runat="server" Text="0.00" ID="txtTNTFuleCharge" Enabled="false" /></td><td>Current Fule Rate</td><td><asp:TextBox runat="server" ID="txtTNTFuleRate" Text="0.00" onkeyup="tntfuleSurchage();" /></td></tr>
                        <tr><td>Total (RM)</td><td><asp:TextBox runat="server" Text="0.00" ID="txtTNTTotal" /></td></tr>
                        <tr><td><asp:Button ID="Button1" runat="server" Text="Button" OnClientClick="javascript:TNTupdateParent();" /></td></tr>
                    </table>
                </fieldset>
            
                <table>
                    <tr><td><asp:Button ID="Button2" runat="server" Text="Button" OnClientClick="javascript:close();" /></td></tr>
                </table>
            
            </div>
    
            
        </form>
    Wednesday, January 21, 2015 10:23 PM

Answers

  • User1711366110 posted

    Hi Renne Hong,
      As per your case , I have been shared my suggestion below :
    1.You can pass the value from child to parent by using Session variable.
    2.If you want to pass more than one value, keep it in array & pass through session variable
    3.In your Child Page script,need to call __doPostBack

    For your reference, Check the below example :
    Parent Form:

    ParentWindow.aspx:
    
    <form id="Form1" method="post" runat="server">
     <input type="button" onclick="window.open('ChildWindow.aspx');" value="Open Child Window">
    </form>
    
    ParentWindow.aspx.cs:
    
    private void Page_Load(object sender, System.EventArgs e)
    {
       // Make sure that the __doPostBack() JavaScript method is created...
     this.GetPostBackEventReference(this, string.Empty);
    
     if ( this.IsPostBack )
     {
      string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
    
      if ( eventTarget == "ChildWindowPostBack" )
       Response.Write("Session: [" + this.Session["ChildWindowKey"] + "]<br>");
     }
     else
     {
     }
    }

    Child Form :

    ChildWindow.aspx:
    
    <form id="Form1" method="post" runat="server">
     <asp:button id="postBackButton" onclick="postBackButton_Click" runat="server" text="Post Back"></asp:button>
    </form>
    
    <script type="text/javascript">
    
    function closeWindow()
    {
     window.opener.__doPostBack('ChildWindowPostBack', '');
     window.close();
    }
    
    </script>
    
    ChildWindow.aspx.cs:
    
    protected void postBackButton_Click(object sender, EventArgs e)
    {
     this.Session["ChildWindowKey"] = "Value from child window";
    
     this.RegisterStartupScript("CloseWindowScript", "<script type='text/javascript'>closeWindow();</script>");
    }
    

    Note: RegisterOnSubmitStatement, RegisterStartupScript, RegisterClientScriptBlock, etc have changed since version 1.1 and you will get
               a compiler warning with the above.

    --
    with regards,
    Edwin

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 22, 2015 9:30 PM