ASP button + confirmation + server side script + browser tab close, all in one?

Posted on 2008-10-21
Medium Priority
Last Modified: 2010-05-18
Hi All

I would greatly appreciate you input into what Im sure is a simple solution, but for the life of me I cant find the missing glue that holds it together&

I have a ASP page with VB code.  Once the user finishes their transaction, they click an ASP Exit button.  The button generates a confirmation message box (Do you want to exit) and then if the user clicks yes, the intention is that the associated VB code removes all the session variables and then it closes the browser tab.  If the user clicks No then nothing happens.

From googling the topic, I realise that Im mixing client and server based activities, but Im sure that there is a simple solution out there.  There has to be&.

Im happy to amend the VB code to somehow call the java script, or amend the javascript to somehow call the vb code. But the big question is how ?

I thank you in anticipation for your time and attention to this.


On page load the following code runs:
  bExit.Attributes.Add("onclick", "if(confirm('Do you wish to Exit?'))
  {}else{return false}")
The ASP Exit buttons details are as follows:
  <asp:Button ID="bExit" runat="server" Text="Exit"   
   onclick="bExit_Click" />
The corresponding VB code that runs once the Exit button is pushed is:
Protected Sub bExit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Some function to call Java script closeWindow();
    End Sub
The javascript that closes the browser tab is:
<script type="text/javascript">
    function closeWindow() {
        var windowObject = window.self;
        windowObject.opener = window.self;
// -->

Open in new window

Question by:PhatMan007
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
LVL 10

Expert Comment

ID: 22765313
Logically, after the page is posted back to server, then they will process everything from server, then render them back to client side. (at that moment your javascript will be execute once again).  There are several tricky way to force, but I dont think you should go for that.

A very common method is render a piece of javascript (this will be very helpful for another thing) just by using:

--->at:  Some function to call Java script closeWindow();
Response.Write("<script> closeWindow(); </script>")

When page is render back to client, this javascript will be parsed and called.

Hope this helps



Expert Comment

ID: 22765344
You do need to mix client side code with server side. The reason for client side code in this case is obvious, you need to prompt the user whether they really want to exit or not.
For this , the bExit needs a javascript click handler which will prompt the user. If the user says yes then it will allow the button to post to the server and execute the server side click method.

Server side method should then do its business, clean up the session etc on serverside and then send a response back to the client. the response should contain the new javascript to close the browser window.

you should not use Response.Write to include script in the page. Instead use     Page.ClientScript.RegisterClientScriptBlock
LVL 10

Expert Comment

ID: 22765406
Of course I would recommend using RegisterStartUpScript if you want to define that javascript function only as user clicks exit button.

But this case, the script is just used to shutdown the current window ;) I think Response.write is enough.

But ofcourse I agree RegisterStartupScript is a better way.

Just give it a try
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 22765603
Hi Jinn

Thanks for your feedback.  I have inserted the Response.Write("<script> closeWindow(); </script>")
as suggested and receive the following error.
Parser Error
Description: An error occurred during the parsing of a resource required to service this request. Please review the following specific parse error details and modify your source file appropriately.

Parser Error Message: Only Content controls are allowed directly in a content page that contains Content controls.

Any idea how this is resolved.?

If I were to try the RegisterStartupScript approach, can you please provide me with a clue as to where and how I include this script into my page.

Many thanks


LVL 10

Expert Comment

ID: 22765684
It seem like your closeWindow(); function is not within the page scope (if you use Master Page, please make sure you define the javascript function within your <asp:Content tag>

If you want to use RegisterStartupScript, this function is used to emits a client-side script block in the page response, it render back a peice of javascript to client browser:

Try out the code, please check the syntax cause I am not sure if I made any typo


Protected Sub bExit_Click(ByVal sender As Object, ByVal e As System.EventArgs)
    'or Session.abandon();   
    If (Not Me.IsStartupScriptRegistered("ShutDownSc")) Then
       ' Form the script to be registered at client side.
        Dim scriptString As String = "<script language=""JavaScript""> function closeWindow() {"
       scriptString += "var windowObject = window.self; windowObject.opener = window.self;  windowObject.close(); }"
       scriptString += " function Page_Load(){ closeWindow();} "
       scriptString += "</script>"
       Me.RegisterStartupScript("ShutDownSc", scriptString)
     End If

Open in new window


Author Comment

ID: 22765801
Hi Jinn

Thanks for such a fast response with the code.  I have inserted it into the page and still get the same error (ie. Only Content controls are allowed.)   Whilst in Visual web developer the 3rd last line of code from the bottom

scriptString += "</script>"
 is underlined with a blue squiggle and if I hover my mouse over it it shows the same asp error

I am using master page and the closeWindow(); function within the <asp:Content tag> and have set the coding language for the page as VB in the <%@ Page ... llanguage="VB" ...%> line at the top of the page.

I have to run now but look forward to any feedback you may have in the morning


LVL 10

Accepted Solution

jinn_hnnl earned 1500 total points
ID: 22774082

That's strange, because that piece of code was just concatenating the javascript string. can you remove that line, compile it and put it back, compile it again (sometimes VS screws up).

Just make sure you have scriptString right, then the exit button should act accordingly

Can you give another try?



Author Comment

ID: 22881462
Hi Jinn

Thanks for the input.  I've had to further break up the string as follows to prevent Viual Studio 2008 from incorectly reconising the </script> portion of the string as the end of the web pages's script section.  

scriptString += "</sc"
scriptString += "ript>"

Thanks for your help.
LVL 10

Expert Comment

ID: 22883848
Glad you worked it out


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The viewer will learn how to implement Singleton Design Pattern in Java.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month12 days, 13 hours left to enroll

777 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