Solved

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

Posted on 2008-10-21
9
1,193 Views
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.

James.


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)

        Session.Remove("DocumentID")

        Session.Remove("IPRefID")

        Session.Remove("NoIPRows")

        Session.Remove("ContainsNoIP")

        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;

        windowObject.close();

    }

// -->

</script>

Open in new window

0
Comment
Question by:PhatMan007
  • 5
  • 3
9 Comments
 
LVL 10

Expert Comment

by:jinn_hnnl
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

JINN


0
 
LVL 6

Expert Comment

by:AmarIs26
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
            Page.ClientScript.RegisterStartupScript
0
 
LVL 10

Expert Comment

by:jinn_hnnl
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
0
 

Author Comment

by:PhatMan007
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

James


0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 10

Expert Comment

by:jinn_hnnl
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

JINN










Protected Sub bExit_Click(ByVal sender As Object, ByVal e As System.EventArgs)

     

    Session.Clear();

    '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

0
 

Author Comment

by:PhatMan007
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

Thanks

James
0
 
LVL 10

Accepted Solution

by:
jinn_hnnl earned 500 total points
ID: 22774082
HI,

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?

JINN

0
 

Author Comment

by:PhatMan007
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.
0
 
LVL 10

Expert Comment

by:jinn_hnnl
ID: 22883848
Glad you worked it out

JINN ^^
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

706 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

15 Experts available now in Live!

Get 1:1 Help Now