ptslv
asked on
How to get a popup window to close
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.loca tion = parent.opener.window.locat ion.href;w indow.clos e();";
strjscript = strjscript + "</script" + ">";
ptslv
string strjscript = "<script language= 'Javascript'>";
strjscript = strjscript + "parent.opener.window.loca
strjscript = strjscript + "</script" + ">";
ptslv
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
How are you currently writing the script block to your output ?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.loca tion.reloa d(true); window.close();";
strjscript = strjscript + "</script" + ">";
ptslv
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.loca
strjscript = strjscript + "</script" + ">";
ptslv
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?
So, the popup shows, you then do an action on it, it then posts, refreshes with a "Done" page containing the JS?
ASKER
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.ToShortDateSt ring()+" "+DateTime.Now.ToShortTime String();
if(!IsPostBack)
{
myMSEL.Text=Request.QueryS tring["MSE L"];
myEntryOrder.Text=Request. QueryStrin g["EntryOr der"];
myPTNumber.Text=Request.Qu eryString[ "PTNumber" ];
CUsers user =new CUsers();
user=(CUsers)Session["Sess ionUser"];
LoginID.Text=user.UserID;
TestPhase.Text=user.TestPh ase;
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(strConnect ion);
string strSQL;
DataSet ds = new DataSet();
strSQL="SELECT Top 1 ......";
try
{
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,co n);
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="Submitti ng 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(strConnect ion);
string strSQL="SELECT .......";
try
{
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText=strSQL;
con.Open();
count=(int)cmd.ExecuteScal ar();
if (count<1)
{
bNotInDB=true;
}
}
finally
{
if(con.State.ToString() == "Open")
con.Close();
}
return bNotInDB;
}
private void HelpDeskSelectedIndexChang ed(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_SelectedIndexChange d(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_SelectedIndex Changed(ob ject sender, System.EventArgs e)
{
if(useShadowing.SelectedVa lue=="Yes" )
{
....set fields enabled to false
}
else
{
set fields enabled to true
}
...set page focus to field ....
}
private void AssumeControl_SelectedInde xChanged(o bject sender, System.EventArgs e)
{
}
public void UpdateTaskRecord()
{
OleDbConnection con = new OleDbConnection(strConnect ion);
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.loca tion.reloa d(true); window.close();";
// strjscript = strjscript + "</script" + ">";
}
private void ClosePage()
{
string strjscript = "<script language= 'Javascript'>";
// strjscript = strjscript + "parent.opener.window.loca tion.reloa d(true); window.close();";
strjscript = strjscript + "window.close();";
strjscript = strjscript + "</script" + ">";
}
private void Page_Load(object sender, System.EventArgs e)
{
DateSubmit.Text= DateTime.Now.ToShortDateSt
if(!IsPostBack)
{
myMSEL.Text=Request.QueryS
myEntryOrder.Text=Request.
myPTNumber.Text=Request.Qu
CUsers user =new CUsers();
user=(CUsers)Session["Sess
LoginID.Text=user.UserID;
TestPhase.Text=user.TestPh
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==
{
EndDate.Text="";
EndTime.Text="";
}
bool bIsError =false;
OleDbConnection con = new OleDbConnection(strConnect
string strSQL;
DataSet ds = new DataSet();
strSQL="SELECT Top 1 ......";
try
{
OleDbDataAdapter da = new OleDbDataAdapter(strSQL,co
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="Submitti
lblErrorMsg.Visible=true;
}
}
private bool CheckIfNewRecord()
{
bool bNotInDB=false;
int count=0;
OleDbConnection con = new OleDbConnection(strConnect
string strSQL="SELECT .......";
try
{
OleDbCommand cmd = con.CreateCommand();
cmd.CommandText=strSQL;
con.Open();
count=(int)cmd.ExecuteScal
if (count<1)
{
bNotInDB=true;
}
}
finally
{
if(con.State.ToString() == "Open")
con.Close();
}
return bNotInDB;
}
private void HelpDeskSelectedIndexChang
{
if(HelpDesk.SelectedIndex=
{
....do something
}
else
{
.....do something else
}
}
private void TIROPENChanged(object sender, System.EventArgs e)
{
if(TIROPEN.SelectedValue==
{
....set fields enabled to false
}
else
{
set fields enabled to true
}
...set page focus to field ....
}
private void Invoke_SelectedIndexChange
{
if(Invoke.SelectedValue=="
{
....set fields enabled to false
}
else
{
set fields enabled to true
}
...set page focus to field ....
}
private void UseShadowing_SelectedIndex
{
if(useShadowing.SelectedVa
{
....set fields enabled to false
}
else
{
set fields enabled to true
}
...set page focus to field ....
}
private void AssumeControl_SelectedInde
{
}
public void UpdateTaskRecord()
{
OleDbConnection con = new OleDbConnection(strConnect
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.loca
// strjscript = strjscript + "</script" + ">";
}
private void ClosePage()
{
string strjscript = "<script language= 'Javascript'>";
// strjscript = strjscript + "parent.opener.window.loca
strjscript = strjscript + "window.close();";
strjscript = strjscript + "</script" + ">";
}
Can you do a view source on the completed popup (when it's ready to be closed) and post it here?
ASKER
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 ("closeWin dowScript" , 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
StringBuilder sb = new StringBuilder();
sb.Append("<script language=\"javascript\" type=\"text/javascript\">"
sb.Append("self.close();")
sb.Append("</script>");
Page.RegisterStartupScript
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
ASKER
So far as I know the refresh works.
My window still won't close. Do I need to use RegisterClientScriptBlock(