ASP.NET - How do you get a value from a popup form in code behind (asp.net c#).

Asked By Eric Smith on 19-Jul-13 04:25 PM
Here is what I am trying to do.  I have web page and I have a button called btnEditDistribution (this is a content page, site wide master page used).  When a user clicks on btnEditDistribution it executes some code if it reaches a certain test condition (case 1:) i need a popup window, to displaying a certain message and determine if user click Yes or No button and let the calling page know and then continue process the rest of the code in btnEditDistribution.  I can't seem to get the popup window to come up


protected void btnEditDistribution_Click(object sender, EventArgs e)
     {
       if (lblVendorID.Text == "")
       {
         lblResults.Text = "Please no vendor rebate record to work with!";
       }
       else
       {
         if (ddlRebateType.SelectedValue.ToString().Trim() == "Chargeback")
         {
           REB.DocType = "5";
         }
         else
         {
           REB.DocType = "1";
         }
  
         //check to see if record exists in PREF200 table
         switch (intDistExists)
         {
           case 0:
             //check to see if record already exists
             int check = REB.DuplicateCheck(lblVendorID.Text.Trim(), txtRebateNumber.Text.Trim());
  
             if (check > 0)
             
               lblResults.Text = "There already exists a record for " + " " + lblVenName.Text.Trim() + " " + " for Rebate Number: " + " " + txtRebateNumber.Text.Trim();
               lblResults.ForeColor = System.Drawing.Color.Red;
               return;
             }
             break;
           case 1:
        need popup window/confimration window to display
        grab yes or no return that here
        if yes run this code  string popupScript = "<script language="'javascript'">" +
                       "window.open('popup.aspx', 'ThisPopUp', " +
                        "'left = 300, top=150, width=400, height=300, " + 
                        "menubar=no, scrollbars=no, resizable=no')" + "</script>";
              Page.ClientScript.RegisterStartupScript(GetType(), "PopupScript", popupScript);
        //RDB.DeleteRebateBatch(lblVendorID.Text.Trim(), txtRebateNumber.Text.Trim());
            //    RDB.UpdatePref100BatchNumb(lblVendorID.Text.Trim(), txtRebateNumber.Text.Trim(), "");
            //    RDB.UpdatePref100DistStatus(lblVendorID.Text.Trim(), txtRebateNumber.Text.Trim(), 0);
            //    CheckFlags();
 
     //window app coded need converted to web based
 //DialogResult res = MessageBox.Show("The distribution is finalized. In order to make changes you need to unfinalize the distribution. Do you wish to unfinalize the distribution?", "Unfinalize Distribution", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
  
             //if (res == DialogResult.Yes)
             //{
             //    RDB.DeleteRebateBatch(lblVendorID.Text.Trim(), txtRebateNumber.Text.Trim());
             //    RDB.UpdatePref100BatchNumb(lblVendorID.Text.Trim(), txtRebateNumber.Text.Trim(), "");
             //    RDB.UpdatePref100DistStatus(lblVendorID.Text.Trim(), txtRebateNumber.Text.Trim(), 0);
             //    CheckFlags(" ");
             //}
             break;
         }
  
  
         //having the $ in the rebate amt will throw format error when inserting rec into database
         string reb = "";
         string dsign = txtRebateAmt.Text.Substring(0, 1);
  
         if (dsign == "$")
         {
           //remove the $
           reb = txtRebateAmt.Text.Remove(0, 1);
           Session["PrefDetailsRebateAmt"] = reb;
         }
         else
         {
           reb = txtRebateAmt.Text.Trim();
           Session["PrefDetailsRebateAmt"] = reb;
         }
         Server.Transfer("PrefDetails.aspx");
       }
     }

Robbe Morris replied to Eric Smith on 19-Jul-13 04:53 PM
The code behind of your parent page does not have a context to the popped up window.  You'll have to return the value from the pop up to the parent page "client side" and then post it back to the server side code on the parent page.  Typically, this is done by populating a hidden field in the parent page with JavaScript.

window.opener in the pop up page will give you a context to the parent page.

window.opener.document.getElementById('some id on the parent page');
Eric Smith replied to Robbe Morris on 22-Jul-13 12:41 PM
On my parent page (EditRebate.aspx), I used the below javascript code. I also placed a HiddenField on the parent page. ( <asp:HiddenField ID="HiddenField1" runat="server" />). EditRebate.aspx has a master page not sure if this makes a difference. I got the popup page showing. <asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
 
<script type="text/javascript">
  function openWindow() {
    window.open("FinalizeConfirm.aspx", '', 'width=310,height=140');
  }
  function setTextBox(value) {
    document.getElementById('HiddenField1').value = value;
  }
  </script> code for popup page Here is the javascript code I am trying on popup to grab value that I need sent to parent page.
<script type="text/javascript">
    function submitData(value) {
      window.opener.setTextBox(value);
      window.close();
    }
  </script> code behind (c#) - setting the value to be passed to parent page
protected void btnYes_Click(object sender, EventArgs e)
     {
       string strAnswer = "YES";
       ScriptManager.RegisterStartupScript(this, this.GetType(), "windowscript", "submitData(" + strAnswer + ")", true);     
     }
 
     protected void btnNo_Click(object sender, EventArgs e)
     {
       string strAnswer = "NO";
       ScriptManager.RegisterStartupScript(this, this.GetType(), "windowscript", "submitData(" + strAnswer + ")", true);
     }
code behing on parent page to test the value pasted from child page. It's not receiving the value from child form. I am not sure where I am off in my code.
case 0:
             ScriptManager.RegisterStartupScript(this, this.GetType(), "windowScript", "openWindow()", true);
 
            if(HiddenField1.Value == "YES" || HiddenField1.Value == "NO")
            {
              lblStatus3.Visible = true;
              lblStatus3.Text = "Got value from popup window!";
            }
            break;