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

Posted on 2008-10-21
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
  • 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.


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 500 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

803 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