ASP.Net Web Forms are divided into two sections: the user interface and the application logic. The user interface comprises HTML markup and ASP.Net web controls whereas the application logic contains all the programming code that allows the controls to interact with themselves and the server back-end. This provides the level of abstractions required for dynamic efficient Web application design. The interface can be altered without any backward or forward dependence on the code (logic). The Web Form interface should be created with the .aspx extension. The application logic can be contained in-line within the ASPX application, but developers should strive to keep the code in a separate location known as the "CodeBehind". This is a file that contains the logic (code) for the Web Form which should end in an extension appropriate for the programming language used, such as .vb for Visual Basic or .cs for C#. The CodeBehind can be written in any language for which there is a .Net compiler for. The compiler generates bytecode that can be deployed anywhere the .Net runtime exists.
The ASPX Web Form is compiled into an object that takes its place on a tree of controls and classes. The CodeBehind is compiled into an object on this tree when the page is requested. The Web Form must inherit from a "base-class" defined in the CodeBehind class file.
Figure14.17.ASPX Web Form class hierarchy
Now we will create a new Web Form containing an HTML heading and an ASP.Net DataGrid control that will display results from the local Virtuoso server.
Example14.50.Sample .Net Web Application: VirtTest.aspx
<%@ Page Language="vb" Inherits="VirtTest" Src="VirtTest.aspx.vb" %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>Virtuoso Provider to DataGrid in VB</title> </head> <body> <h1>Simple VB Virtuoso DataBinding Demo</h1> <form xml:id="form1" method="post" runat="server"> <asp:DataGrid id=DataGrid1 runat="server" DataMember="Customers" BorderColor="silver" BorderWidth="1" CellPadding="2" Font-Name="Tahoma" Font-Size="10pt"> <HeaderStyle BackColor="#2222ff" ForeColor="yellow"/> <PagerStyle Mode="NextPrev" /> </asp:DataGrid> </form> <hr> </body> </html>
On the first line of the file we use the
@ page directive to inherit the CodeBehind
class we will create in the CodeBehind file. The source of which
can be optionally declared here with the
Src= attribute. Visual Studio makes use of
CodeBehind= for tracking
associated source code instead. If either of these two attributes
are specified then the .Net runtime will attempt to compile the
code into an assembly (DLL) upon first execution if it does not
already exist or seems out-of-date. The following code fragment is
the CodeBehind, VirtTest.aspx.vb.
Now we want some logic behind the Web Form. We have placed a DataGrid control on a web page but we need to supply data to it now:
Example14.51.Sample .Net Web Application CodeBehind: VirtTest.aspx.vb
imports System imports System.Web imports System.Web.UI imports System.Web.UI.WebControls imports System.Web.UI.HtmlControls imports System.Data imports OpenLink.Data.VirtuosoClient Public Class VirtTest Inherits Page Protected WithEvents myConnection As OpenLink.Data.VirtuosoClient.VirtuosoConnection Protected WithEvents myCommand As OpenLink.Data.VirtuosoClient.VirtuosoDataAdapter Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid Protected WithEvents ds As System.Data.DataSet Private Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim myConnection As new VirtuosoConnection("HOST=noodle:1112;UID=dba;PWD=dba;Database=Demo") Dim myCommand As new VirtuosoDataAdapter("select * from Demo..Customers", myConnection) Dim ds As new DataSet() myCommand.Fill(ds, "Customers") DataGrid1.DataSource=ds.Tables("Customers") DataGrid1.DataBind() End Sub End Class
This file is compiled using the following command (split across lines for readability and would otherwise all be on one line):
E:\myweb\myapp> vbc /target:library /r:System.dll /r: System.Web.dll /r:System.Data.dll /r:OpenLink.Data.VirtuosoClient.dll /r:System.Xml.dll /out:bin\VirtTest.dll VirtTest.aspx.vb
Once compiled, the resulting DLL library should be in the bin
subdirectory of the application root. When the assembly is
available in this way the .aspx file does not need to contain the
Src= attribute since there will
be no need to compile the CodeBehind. If the
Src= attribute is specified in the .aspx
file but the CodeBehind file is not located on the server then an
error will be signalled indicating that a required resource cannot
be found, because the CodeBehind class is compiled on demand.