Solved

How to get a popup window to close

Posted on 2006-07-11
9
198 Views
Last Modified: 2010-08-05
I am trying to close a web page popup form to get back to the parent window after data is submitted.  The window remains open and I don't get any errors on the code.  I am using C#.  Here is my code:

string strjscript = "<script language= 'Javascript'>";
strjscript = strjscript + "parent.opener.window.location = parent.opener.window.location.href;window.close();";
strjscript = strjscript + "</script" + ">";

ptslv
0
Comment
Question by:ptslv
  • 4
  • 3
  • 2
9 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 125 total points
ID: 17084832
This is a "it may work" idea, try:

    strjscript = strjscript + "parent.opener.window.location = parent.opener.window.location.href; self.close();";

Just out of curiosity, does the refresh work ?
0
 

Author Comment

by:ptslv
ID: 17084997
carl,

So far as I know the refresh works.  

My window still won't close.  Do I need to use RegisterClientScriptBlock()?  I noticed I have that remmed out in my code, but it doesn't work with that either.
0
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 17085066
How are you currently writing the script block to your output ?
0
 
LVL 26

Assisted Solution

by:DireOrbAnt
DireOrbAnt earned 125 total points
ID: 17087841
That code is on the resulting page, after the data is posted?

Try:
strjscript = strjscript + "window.close();";

Why do you need:
parent.opener.window.location = parent.opener.window.location.href; ?

Do you want to reload the parent window?  If so, try:
strjscript = strjscript + "parent.opener.window.location.reload(true); window.close();";
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:ptslv
ID: 17090738
I rechecked and found that RegisterScriptBlock is on the parent page.  I have no problem going from the parent page to the popup with passing my data that I need.  On the popup, my data submits fine and I can update the tables.  The parent window remains open in the background so the user can reference it if needed.

I changed the strjscript to what Dire said, but that still does not close the popup window.  This is what I have now:

string strjscript = "<script language= 'Javascript'>";
strjscript = strjscript + "parent.opener.window.location.reload(true); window.close();";
strjscript = strjscript + "</script" + ">";

ptslv
0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17092719
Does the parent reload work? If not simply put window.close() and see if it closes it.

So, the popup shows, you then do an action on it, it then posts, refreshes with a "Done" page containing the JS?
0
 

Author Comment

by:ptslv
ID: 17092994
OK.  I tried everything, including putting the window.close() in a separate function and calling it after all data processing is completed.  Nothing is working to close the popup form.  Here is all my code for the popup page:

private void Page_Load(object sender, System.EventArgs e)
            {
                  DateSubmit.Text= DateTime.Now.ToShortDateString()+" "+DateTime.Now.ToShortTimeString();

                  if(!IsPostBack)
                  {
                        myMSEL.Text=Request.QueryString["MSEL"];
                        myEntryOrder.Text=Request.QueryString["EntryOrder"];
                        myPTNumber.Text=Request.QueryString["PTNumber"];

                        CUsers user =new CUsers();
                        user=(CUsers)Session["SessionUser"];
                        LoginID.Text=user.UserID;
                        TestPhase.Text=user.TestPhase;
                        TestPhase.DataBind();
                        LoadWorkStationData();
                        GetSiteData();
                        SetTIRNumber(LoginID.Text);
                  }
            }

            private void LoadWorkStationData()
            {
                  .....gets data and binds to field
            }

            private void GetSiteData()
            {
                  .....gets data and binds to field

            }


            private void SetTIRNumber(string LoginID)
            {
                  .....dynamically sets TIR number
            }

            private void HandleErrors()
            {
                  ...do error handling
            }

            #region Web Form Designer generated code
            
            #endregion

            private void btnSubmit_Click(object sender, System.EventArgs e)
            {
                  if(CheckIfNewRecord())
                  {
                        if(TIROPEN.SelectedValue=="Yes")
                        {
                              EndDate.Text="";
                              EndTime.Text="";
                        }
                        bool bIsError =false;
                        OleDbConnection con = new OleDbConnection(strConnection);
                        
                        string strSQL;
                        DataSet ds = new DataSet();
                        
                        strSQL="SELECT Top 1 ......";
            
                        try
                        {
                              OleDbDataAdapter da = new OleDbDataAdapter(strSQL,con);
                              da.Fill(ds);
                              DataTable mytable =ds.Tables[0];
                              DataRow myRow ;
                              myRow = mytable.NewRow();
                              string n ;
                              foreach(DataColumn c in mytable.Columns)
                              {
                                    ....do something
                              }
                              mytable.Rows.Add(myRow);
                              OleDbCommandBuilder cb = new OleDbCommandBuilder(da);
                              da.Update(ds);
                              con.Close();
                              UpdateTaskRecord();
                              
                        }
                        catch(Exception err)
                        {
                              string str=err.Message;
                              bIsError=true;
                        }
                        finally
                        {
                              if(con.State.ToString() == "Open")
                                    con.Close();
                        }


                        if(bIsError)
                        {      
                              Panel1.Visible=false;
                              lblErrorMsg.Visible=true;
                        }
                        else
                        {
                              ClosePage();  //********* FUNCTION TO CLOSE THE PAGE
                        }
                  }
                  else
                  {
                        Panel1.Visible=false;
                        lblErrorMsg.Text="Submitting this record would create a duplicate record in the database and therefore, it will not be submitted.";
                        lblErrorMsg.Visible=true;
                  }
            }


            private bool CheckIfNewRecord()
            {
                  bool bNotInDB=false;
                  int count=0;
                  OleDbConnection con = new OleDbConnection(strConnection);
                  
                  string strSQL="SELECT .......";

                  try
                  {
                        OleDbCommand cmd = con.CreateCommand();
                        cmd.CommandText=strSQL;
                        con.Open();
                        count=(int)cmd.ExecuteScalar();

                        if (count<1)
                        {
                              bNotInDB=true;
                        }
                  }
                  finally
                  {
                        if(con.State.ToString() == "Open")
                              con.Close();
                  }
                  return bNotInDB;
            }

            
            private void HelpDeskSelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if(HelpDesk.SelectedIndex==0)
                  {
                        ....do something
                  }
                  else
                  {
                        .....do something else
                  }
            }

            private void TIROPENChanged(object sender, System.EventArgs e)
            {
                  if(TIROPEN.SelectedValue=="Yes")
                  {
                        ....set fields enabled to false
                  }
                  else
                  {
                        set fields enabled to true      
                        
                  }
                  
                  ...set page focus to field ....
                  
            }

            private void Invoke_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if(Invoke.SelectedValue=="Yes")
                  {
                        ....set fields enabled to false
                  }
                  else
                  {
                        set fields enabled to true      
                        
                  }
                  
                  ...set page focus to field ....
            }

            private void UseShadowing_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  if(useShadowing.SelectedValue=="Yes")
                  {
                        ....set fields enabled to false
                  }
                  else
                  {
                        set fields enabled to true      
                        
                  }
                  
                  ...set page focus to field ....
            }

            private void AssumeControl_SelectedIndexChanged(object sender, System.EventArgs e)
            {
            
            }

            public void UpdateTaskRecord()
            {
                  OleDbConnection con = new OleDbConnection(strConnection);
                  try
                  {
                        string strUpdate ="Update ........";
                        con.Open();
                        OleDbCommand cmd2 = new OleDbCommand(strUpdate, con);
                        cmd2.ExecuteNonQuery();
                        con.Close();
                  }
                  catch (Exception err)
                  {
                        string s = err.Message;
                        lblErrorMsg2.Text = "** "+ s;
                        lblErrorMsg2.Visible=true;
                  }
                  finally
                  {
                        if(con.State.ToString() == "Open")
                        {
                              con.Close();
                        }
                        
                  }
                  
                  // WHERE I INITIALLY CALLED WINDOW.CLOSE()
                  
//                  string strjscript = "<script language= 'Javascript'>";
//                  strjscript = strjscript + "parent.opener.window.location.reload(true); window.close();";
//                  strjscript = strjscript + "</script" + ">";
 
            }

            private void ClosePage()
            {
                  string strjscript = "<script language= 'Javascript'>";
//                  strjscript = strjscript + "parent.opener.window.location.reload(true); window.close();";
                  strjscript = strjscript + "window.close();";
                  
                  strjscript = strjscript + "</script" + ">";
            }


0
 
LVL 26

Expert Comment

by:DireOrbAnt
ID: 17093037
Can you do a view source on the completed popup (when it's ready to be closed) and post it here?
0
 

Author Comment

by:ptslv
ID: 17093930
Well, everyone,  I finally got it to work.  I used scriptbuilder to build the javascript in my ClosePage function and used Page.RegisterStartupScript:

StringBuilder sb = new StringBuilder();
sb.Append("<script language=\"javascript\" type=\"text/javascript\">" + Environment.NewLine);
sb.Append("self.close();");
sb.Append("</script>");

Page.RegisterStartupScript("closeWindowScript", sb.ToString());


Thanks for all the help though.  I appreciate it.  Still don't understand why the other script didn't work, unless it needed to be registered....

ptslv
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
New to SOAP and need some direction 10 45
Replace &lt; with < 14 56
Interview questions for support of a legacy ASP.NET site 4 38
C# guarantee sql connection close 6 13
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now