Hi all,

I am unable to write the code for EXIT button like displaying a message box for confirmation and if user clicks on OK then the application should be closed and if the user selects CANCEL then he should be in the same page..

I tried using Me.Close() but i am getting an error.. any suggestions or help is greatly appreciated...


THanks in advance,
Karthik.

What do u mean by exiting the application? You mean closing the browser window on button click?

What do u mean by exiting the application? You mean closing the browser window on button click?

Hi Aneesh,

Thanks for your reply. Ya like when the user clicks on the Exit button the browser window should be asking for a confirmation message and then if the user clicks on Ok buton then the window should be closed.

Thanks,
Karthik.

Try this code

string _scriptCloseDialog = "<script>window.close();</script>";

ClientScript.RegisterStartupScript(GetType(), "_load", _scriptCloseDialog);

Oops that was C#
Here is the VB Code

Dim _scriptCloseDialog As String
_scriptCloseDialog = "<script>window.close();</script>"
ClientScript.RegisterStartupScript([GetType], "Close", _scriptCloseDialog)

THanks for your reply aneesh but this code is not working in my button click event.
i have written some code

Dim response As MsgBoxResult
    response = MsgBox("Do you want to close form?", MsgBoxStyle.Question + MsgBoxStyle.YesNo, "Confirm")
    If response = MsgBoxResult.Yes Then
        Me.Dispose()
    ElseIf response = MsgBoxResult.No Then
        Exit Sub
    End If

but if i click on ok button it is in the same page
thanks in advance.
karthik.

The code you have posted is for closing a window application form. I thought you were doing a web application in Asp.net. You cannot close a browser window using a server side scripting language like VB. Instead you have to use any client side scripting language like javascript to accomplish this task. By the way in which browser did you test my code. Try testing the code in IE and let me know your feedback. Dont worry about the confirmation message. The browser will show it automatically.

The code you have posted is for closing a window application form. I thought you were doing a web application in Asp.net. You cannot close a browser window using a server side scripting language like VB. Instead you have to use any client side scripting language like javascript to accomplish this task. By the way in which browser did you test my code. Try testing the code in IE and let me know your feedback. Dont worry about the confirmation message. The browser will show it automatically.

ya your code is working if i paste it in page_load but soon after the page is loaded it is asking for a confirmation message and if i click on Yes then it is closing the browser completely but if i paste the same code in exit button then there is no response for the button click.

Ok so how to write client side script to close the window?
Yes I have tried in IE only but nothing is happening if i click on the Exit button

Thanks,
Karthik.

That means your button is not coded properly. Check the Click function of your button is handled by the same function you have pasted your code. I suggest you to try it on a new button after deleting the old button.

Your button source should look similar to this:

<asp:Button ID="Button1" runat="server" Text="Exit" />

And the button Click should look like this:

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim strscript As String = "<script language=javascript>window.top.close();</script>"
If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
ClientScript.RegisterStartupScript(Page.GetType(), "clientScript", strscript)
End If
End Sub

Ya Thanks alot for your help aneesh cud u please explain me the code behind the lines u have written?

Thanks its working

Hi Aneesh,

THe code for the exit button is working fine in my computer but when i view it in other system then i am not able to view the functionality of the exit button properly means that if i click on the exit button then my form is getting redirected to the edit page and thats a problem

Thanks in advance,
Karthik.

Thats strange, Can u show me ur code both button source and the code behind?

Hi Aneesh,
Here is my code:

This is in .aspx.vb

strscript As String = "<script language=javascript>window.top.close();</script>"
        If (Not ClientScript.IsStartupScriptRegistered("clientScript")) Then
            ClientScript.RegisterStartupScript(Page.GetType(), "clientScript", strscript)
        End If

and in aspx

<asp:Button ID="btnexit" runat="server" Style="z-index: 149; left: 324px; position: absolute;
top: 674px" Text="EXIT" Font-Bold="True" />

i dont know where is the problem in my code?

Thanks in advance,
Karthik.

I can see only two reasons why this does not work in IE
1) Make sure ur code is in the correct button click event
2) Make sure javascript is enabled in the client browser settings
It wont work in Firefox if the window is not opened through script(I will explain it later. First let us fix this in IE).
Its working fine for me!

ya thanks aneesh its working earlier i used to view it in FF now i have tried the same in IE its working..............

can u suggest me how to include a designed webpage into a master page??

thanks in advance,
Karthik.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.