'Visual Basic Web Form Close() Method Not Working
Form Design
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
Me.Close()
End Sub
End Class
Form Design =screenshot of the Web Form I am trying to design
I am trying to get the close button to work. When I created an event procedure for the button to work, I assumed the Me.Close() method would work, but an error occurred and it doesn't seem to be recognized. Does anyone know how to actually get the web form / window to close when the close button is clicked?
Sorry about formatting, this is my first question asked on here.
Solution 1:[1]
Well, in desktop land, we can do this:
form1: click button - open form2
In form2, click on button, close form (that would be form2), and THEN we return to form1.
In web land, you don't have the above "approach" anymore.
In web land?
Form1: click button JUMP or "navagte" to form2.
At this point in time, we are now on web page and URL form2.
You can't close the web browser (as a general rule).
So, if you want to close the Form2, you navigate and jump BACK to Form1.
So, it will look like this:
So say on form 1 we have this:
<br />
<h2>Click on button to pick favorte food</h2>
<asp:Button ID="Button1" runat="server" Text="Jump to Form2 to pick food" />
<br />
<br />
<h2>Favorate food picked</h2>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
We get this:
So, lets wire up the button click
So, we double click on that button and in code behind we write this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Session("FavorateFood") Is Nothing Then
Label1.Text = Session("FavorateFood")
End If
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Response.Redirect("WebForm2.aspx")
End Sub
So, we click on the button, and jump to Form2.
We have this on that page:
<h2>Please pick your favorate food</h2>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem>Pork Chops</asp:ListItem>
<asp:ListItem>Cherry Pie</asp:ListItem>
<asp:ListItem>Lobster</asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="Button1" runat="server" Text="Done, return back to Form1" />
And we see this:
And the code behind for that button click is this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Session("FavorateFood") = RadioButtonList1.SelectedItem.Text
Response.Redirect("WebForm1.aspx")
End Sub
So, we pick a food, but note how we DO NOT have the concept of closing the form, but only that of jumping back (navigating) to the first form.
So, I can pick a food, and click the button, we get this:
so, as a general rule ask this question:
In the past years, when you used a web site, did you EVER click a button, and it closed down your web browser? No, it never does and did. There are RARE cases when you might want to do that, but I can't think of in the last 10 years, that I ever clicked on a button, and it closed the web browser. In fact, users don't like it much when you try to close a web browser - and you often see a prompt that tells you the web site is trying to close your browser - and will you allow it???
so, in web land, the concept of
Open a form
close a form
it does not really exist - you can jump to new web page, but the idea of open and close does not really exist as a concept in web land, and thus your designs and how you approach software has to be changed. In other words, the long time desktop concept of open form, and close form has to be dropped from your design approach here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Albert D. Kallal |




