Building Web Forms with Visual Studio .NET





Building Web Forms with Visual Studio .NET

All the topics discussed in this chapter are directly applicable to building Web forms with the Visual Studio .NET designer; however, there are some subtleties to the way the designer works that you should be aware of.

Figure shows a sample ASP.NET Web application in Microsoft Visual Studio .NET. In this example, a pair of server-side controls has been added with the designer, and a handler has been associated with the Click event of the server-side button. There are two important Page attributes to notice in the generated .aspx file. First, note the Codebehind attribute, whose value is set to the source file for the code-behind. This is not equivalent to the src attribute discussed in Chapter 1, but is a tag used exclusively by Visual Studio .NET to associate source files with .aspx pages (so that they show up as connected in the Solution Explorer and so that the code-behind file can be brought up for any .aspx page by using the F7 key). ASP.NET does not recognize this attribute and ignores it altogether. For the code-behind class to be referenced by a Visual Studio .NET–created page, it must be compiled into an assembly and placed in the /bin directory of the virtual root for that application. This is exactly what Visual Studio .NET does for you. In fact, all code files within a Web application project are compiled into a single assembly, which is placed in the /bin directory of the virtual root associated with that project. The second important attribute to note is AutoEventWireup, which is set to false. This means that your page and code-behind class may not use the automatic event wire-up mechanism for Page events. Instead, you must explicitly register delegates for any events you want to handle, or use the Handles keyword to register handlers for events.

Visual Studio .NET Web Form Application

graphics/02fig10.gif

To complete the picture, Listing 2-15 shows the code-behind file, WebForm1.aspx.vb, that is referenced by the .aspx page in our sample project. Note that when the designer is used to place a server-side control on a form, it takes care to place a protected data member of the appropriate type in the code-behind file. Also note that because AutoEventWireup is disabled from the .aspx page, the code-behind generated by Visual Studio .NET is careful to use the Handles keyword to wire up the Page_Load and Page_Init handlers. Notice that the designer explicitly declares the server-side control member variables using the WithEvents keyword, and it wires up the server-side control event handlers using the Handles keyword.

Listing 2-15 Visual Studio .NET–Generated Code-Behind File
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Namespace WebFormsApp

  Public Class WebForm1
         Inherits Page
    Protected WithEvents _PushMe As Button
    Protected WithEvents _Name As TextBox

    Private Sub Page_Load(ByVal sender As Object,
                          ByVal e As EventArgs) _
                Handles MyBase.Load
     ' Put user code to initialize the page here
    End Sub

#Region "Web Form Designer generated code"
    Private Sub Page_Init(ByVal sender As Object, _
                  ByVal e As EventArgs) _
Handles MyBase.Init
      InitializeComponent()
    End Sub

    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
    End Sub
#End Region
    Private Sub Button1_Click(ByVal sender As Object, _
                              ByVal e As EventArgs) _
            Handles _PushMe.Click
    End Sub
  End Class
End Namespace

A common mistake people often make when they begin working with Visual Studio .NET is to assume that the Codebehind attribute implicitly compiles the code-behind file when the page is accessed. This is not true, and the code-behind file must be explicitly compiled every time it is changed and then deployed to the /bin directory of the application.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows