Web Form Fundamentals
cont’d
HTML Control Classes
Classes in .NET use inheritance for sharing functionalities.
Every HTML server control inherits from base class HtmlControl.
HTML server controls provide properties that closely match their
tag attributes.
HTML Control Events
1. ServerClick (might override the expected behavior e.g.
anchor)
2. ServerChange
Events pass two parameters (sender, EventArgs)
HtmlInputImage
2
HtmlInputImage
Protected Sub ImgButton_ServerClick(ByVal sender As Object,
ByVal e As System.Web.UI.ImageClickEventArgs) Handles
ImgButton.ServerClick
Result.InnerText = "You clicked at (" & e.X.ToString() &
", " & e.Y.ToString() & "). "
If e.Y < 100 And e.Y > 20 And e.X > 20 And e.X < 275 Then
Result.InnerText &= "You clicked on the button surface."
Else
Result.InnerText &= "You clicked the button border."
End If
End Sub
3
HTML Control Classes
HtmlInputSubmit
HtmlInputReset
HtmlInputPassword
HtmlLink
HtmlTitle
HtmlHead
4
HTML ToolBox VS
5
Result of Ongoing Evolution
HtmlControl Base Class
Attributes: collection of tag attributes and their values.
(Useful for adding or config custom attributes).
Controls: collection of all contained controls (System.Web.UI.Control
Ctype function)
Disabled: user cannot interact and events are not fired.
EnableViewState: auto state mgt of control, T/F
Page: reference to web page containing this control.
Parent: reference to control containing this control.
Style: collection of CSS style properties.
TagName: underlying HTML element (e.g. <img>, <div>)
Visible: F, hidden, not rendered
6
HtmlContainerControl Class
Any Html control that requires a closing tag also inherits from
HtmlContainerControl class.
e.g. <a>, <form>, <div>
Properties
1. innerHtml: Html content between opening and closing tags.
2. innerText: Text content between opening and closing tags.
special characters will be automatically converted to
equivalent Html entities and displayed like text.
< <
7
HtmlInputControl Class
This class defines some properties that are common to all the HTML
controls based on the <input> tag.
<input type=“text”>
<input type=“submit”>
<input type=“file”>
Properties
Type: type of input control
Value: returns the contents of control as a string.
8
Page Class
Every web page is a custom class that inherits from page class
System.Web.UI.Page.
Properties
Application and Session: collections, state information on the server
Cache: collection of stored objects for reuse
Controls: collection of all contained controls. {dynamic addition}
EnableViewState: if F, none of the controls maintain state info
IsPostBack: Boolean, first time or resubmitted.
Request: HttpRequest object, cookies, form values
Response: HttpResponse object, set web response, redirect
Server: HttpServerUtility object, misc tasks, url/html encoding.
User (New in FW 2): User information
9
Controls Collection
• Accessing controls collection
• Adding controls dynamically to the the controls collection
10
HttpRequest Class
Encapsulates all info related to a client request for a web page.
Properties
ApplicationPath & PhysicalPath: app virtual dir / real dir
Browser: HttpBrowserCapabilities object, support for ActiveX, cookies, VBScript,
frames
Cookies: gets the collection of cookies
Header & ServerVariables: name/value
QueryString: parameters passed along with query string
Url & UrlReferrer: Url object, current address, previous page
UserAgent: string, browser type, “MSIE”
UserHostAddress & UserHostName: IP address, DNS name of remote client
UserLanguages: sorted string array, language preferences
New Additions
ClientCertificate
IsAuthenticated, IsSecureConnection
11
12
HttpResponse Class
Allows to send info directly to client.
Properties (and Methods):
BufferOutput: If T, page is not sent as piecemeal.
Cache: HttpCachePolicy object, configure caching.
Cookies: collection of cookies sent with response.
Write( ), BinaryWrite( ), WriteFile( ): write text or binary or file
content directly to response stream.
Redirect( ): transfers the user to another page (or to different
site)
13
HttpServerUtility Class
Misc helper methods.
Methods:
CreateObject: create instance of COM object (progID),
backward compatibility.
HtmlEncode: changes a string to legal HTML characters.
HtmlDecode:
UrlEncode: changes a string to legal URL characters.
UrlDecode:
MapPath: returns the physical file path corresponding to a virtual file path
on server.
Transfer: transfer execution to another page in current application.
14
Encode and Decode
ctrl1.InnerHtml = "To <b>bold</b> text use the <b> tag."
ctrl2.InnerHtml = "To <b>bold</b> text use the " +
Server.HtmlEncode("<b>") + " tag.“
URL Encoding
15
Compiled Code-Behind Files
Precompiled .NET assemblies
– Container for compiled code
– vbc, csc
• C:\Windows\Microsoft.NET\Framework[version]\
• v1.0.3705
• v1.1.4322
• v2.0.50727
• v4.0.30319
• C:\Windows\Microsoft.NET\Framework\v4.0.30319>
vbc /t:library /r:System.dll /r:System.Web.dll filename.vb
• Library file is created as an outcome of compilation process.
• What are dependent assemblies.
• Specifying complete paths for source and target (/out) files
• Copy the assembly to \bin of Web Application
• No need to specify src attribute in the page directive.
• Setting Path variable for compiler
• Multiple files in one assembly
– Unique class names/or in different namespace
16
Vb Compiler – vbc.exe
17
Using Compiled Code-Behind File
<!- NOTE Helloworld3.dll is present in \BIN directory of the Web
Application >
<%@ Page Language="VB“ Inherits="HelloWorld" %>
<HTML> app
<BODY> \bin
<FORM id="Form" RUNAT="SERVER">
<asp:Label id="lblTest" RUNAT="SERVER" />
</FORM>
</BODY>
</HTML>
18
Importing Namespaces
Imports System.Web.UI
Imports System.Web.UI.WebControls
Public Class HelloWorld
Inherits Page
Protected lblTest As Label
Public Sub Page_Load()
lblTest.Text="Hello, the Page_Load event occurred."
End Sub
End Class
For .aspx file use the following syntax:
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %> 19
Global Application File (Global.asax)
20
Global Application File
<script runat="server">
Public Sub Application_EndRequest( )
Response.Write("<hr>This page was served at " &
DateTime.Now.ToString())
End Sub
</script>
Writing the same global file using code-behind
21
Application Events
1. Application_Start
2. Application_End
3. Session_Start
4. Session_End
5. Application_Error
6. Application_BeginRequest
7. Application_EndRequest
22
References
Beginning ASP.NET 4.0
Chapter 5
23