Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1227
  • Last Modified:

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

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

  • 5
  • 3
1 Solution
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


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
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
Industry Leaders: 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!

PhatMan007Author Commented:
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


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

PhatMan007Author Commented:
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



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?


PhatMan007Author Commented:
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.
Glad you worked it out


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now