![]() ![]() | ||
Using buttons in Web forms is similar to using buttons in Windows forms, as you can see in the Buttons example on the CD-ROM. You can see this example at work in Figure 15.6; when you click the button labeled "Click me", the text "Hello from Visual Basic" appears in a text box.
For reference, here is WebForm1.aspx from the Buttons example:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind= "WebForm1.aspx.vb" Inherits="Buttons.WebForm1"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <title></title> <meta name="GENERATOR" content="Microsoft Visual Studio.NET 7.0"> <meta name="CODE_LANGUAGE" content="Visual Basic 7.0"> <meta name=vs_defaultClientScript content="JavaScript"> <meta name=vs_targetSchema content="http://schemas.microsoft.com/ intellisense/ie5"> </HEAD> <body MS_POSITIONING="GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Button id=Button1 style="Z-INDEX: 101; LEFT: 95px; POSITION: absolute; TOP: 75px" runat="server" Text="Click me" Width="125px" Height="24px"></asp:Button> <asp:TextBox id=TextBox2 style="Z-INDEX: 104; LEFT: 239px; POSITION: absolute; TOP: 127px" runat="server"></asp:TextBox> <asp:Button id=Button2 style="Z-INDEX: 103; LEFT: 95px; POSITION: absolute; TOP: 125px" runat="server" Text="Click me too" Width="125px" Height="24px" CommandArgument="You clicked Button2" CommandName="Button2"></asp:Button> <asp:TextBox id=TextBox1 style="Z-INDEX: 102; LEFT: 239px; POSITION: absolute; TOP: 76px" runat="server"></asp:TextBox> </form> </body> </HTML>
And here is WebForm1.aspx.vb, which holds the Visual Basic code in the Buttons example:
Public Class WebForm1 Inherits System.Web.UI.Page Protected WithEvents Button1 As System.Web.UI.WebControls.Button Protected WithEvents Button2 As System.Web.UI.WebControls.Button Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox #Region " Web Form Designer Generated Code " 'This call is required by the Web Form Designer. <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent() End Sub Private Sub Page_Init(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Init 'CODEGEN: This method call is required by the Web Form Designer 'Do not modify it using the code editor. InitializeComponent() End Sub #End Region Private Sub Page_Load(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _ System.EventArgs) Handles Button1.Click TextBox1.Text = "Hello from Visual Basic" End Sub Private Sub Button2_Command(ByVal sender As Object, ByVal e As _ System.Web.UI.WebControls.CommandEventArgs) Handles Button2.Command TextBox2.Text = e.CommandArgument End Sub End Class
By default, buttons are supported as HTML Submit buttons; here is the actual HTML sent from the server to the Web browser:
<input type="submit" name="Button1" value="Click me" id="Button1" style="height:24px;width:125px;Z-INDEX: 101; LEFT: 95px; POSITION: absolute; TOP: 75px" />
In the Visual Basic code, you can handle the button's Click event just as you would in a Windows form; just double-click a button to bring up this code in a code designer:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
⋮
End Sub
This is reassuringly like what Windows programmers are used to, even though this button will appear in a Web browser. To display the text in the text box you see in Figure 15.6, you only need to add this code to the event handler and run the application:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles Button1.Click
TextBox1.Text = "Hello from Visual Basic"
End Sub
![]() ![]() | ||