Hey,
I have a web page (web form) that is displaying an image.
I want to close it using a button as well.
What command closes the current web form?
Thanks,
Printable View
Hey,
I have a web page (web form) that is displaying an image.
I want to close it using a button as well.
What command closes the current web form?
Thanks,
You will have to use client-side javascript to close the window
In you HTML you will need to call this ClientSideScript on the onClick event of the button, you want to use as the close buttonCode:<script language="JavaScript">
function CloseWin()
{
window.close();
}
</script>
edit: fixed the previous typos in the word - codeCode:<input type="button" text="Close" onClick="javascript:CloseWin();" ID="BtnClose"
>
Thanks.
However, I'm using VS.NET to develop the web page.
It tells me that java script is not a member of that page.
Am I doing something wrong?
Also, are there any web resources to learn Java script?
It seems that asp alone isn't sufficient to do some neat stuff. (mouse overs, etc..)
THanks,
You will need to edit the HTML code and add that javascript in it.
The javascript is not to be added in the - say - webform1.aspx.vb, file, it should be added in the webform1.aspx file.
When inthe desgin mode, click on the HTML button (lower left hand side corner) and enter the above code in it.
That's what I've done. However, it does not work.
It tells me that java script is not a memeber of the web form.
Thanks,
Please post the exact code you have used and the place where you have put it.
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="Tester.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<code>
<script language= javascript>
function CloseWin()
{
window.close();
}
</script>
</code>
<form id="Form1" method="post" runat="server">
<asp:Button onclick= "javascript:CloseWin()" id="Button1" style="Z-INDEX: 101; LEFT: 315px; POSITION: absolute; TOP: 132px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>
1. I am sorry for the typos in my first post
2. please do the following
I have deleted the <code></code> words because, it was teh foramtting i need here in vbforums and not for teh HTML page.
Second, I have placed teh script block inside the <HEAD> tag
Thrid: removed teh space from between the words "java" and script in the onclick event
Code:<%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm2.aspx.vb" Inherits="Tester.WebForm2"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>WebForm2</title>
<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language= javascript>
function CloseWin()
{
window.close();
}
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:Button onClick= "javaScript:CloseWin();" id="Button1" style="Z-INDEX: 101; LEFT: 315px; POSITION: absolute; TOP: 132px" runat="server"
Text="Button"></asp:Button>
</form>
</body>
</HTML>
Thanks,
I get the following error:
Compiler Error Message: BC30456: 'java' is not a member of 'ASP.WebForm2_aspx'
this is really weird.
please remove all spaces between the words "java" and "script"
It should be ONE word : JavaScript.
I accidentally started a new thread, trying to reply to this thread.:D
I have tried that as well.
Same error except it says that javascript is not a member of web form.aspx.
The other time it said java (I guess it omits anything after the space)
Thanks,
Well, does it tell you any line thats got the error?
If you don't need the button to run any server side code just use a html button instead
Not sure why but i can't get the above line to appear without a space between java and script just remove it.Code:<input type="button" id="btnclosewin" onclick="javascript:closewin();" value="Close window">
Otherwise if you do also want to run server side code aswell as the javascript you can use an <asp:button> like this.
VB Code:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not IsPostBack Then btnClose.Attributes.Add("onclick", "window.close();") End If End Sub
EDIT: NOTE:Unless you opened the window yourself the browser will display a confirmation box asking the user if they want the page to close.
thanks dude.
I was just wondering,
If I add the 'onclick' attribute, would it overwrite any code that is the routine handling the onclick event for the button?
thanks,
Nope any code behind your button would still run.
Though perhaps depending on the javascript as you could add an OnClick attribute that displayed a JS confirmation box and then your code would only run if the user selected Yes/ok but not if they selected no/cancel.
so the code would run first and then it would close the window?
No the window will close first then the code will run.
Can I run my own functions using the Attributes.Add?
Have a read of this MSDN article it should clear things up for you.