Note
Note
When you develop an ASP.NET Web Form, you can use the following
type of controls:
a) HTML Server Controls
You can manipulate these controls at the server-side.
Before dispatching a form to the client, the ASP Engine converts
them to the equivalent HTML elements.
These controls are included in the System.Web.UI.HtmlControls
namespace.
b) Web Server Controls/Web Controls/ASP.NET Web Form
Controls)
These are the new generation’s controls developed by Microsoft.
In the HTML or .aspx file, these are typically referenced with an
asp: prefix such as asp:Label, asp:Button, or asp:TextBox.
The ASP.NET Engine also maps these controls to standard HTML
equivalent controls before dispatching the page to the client.
These Web server controls are available in the
System.Web.UI.WebControls namespace.
c) Validation Controls
This set of controls provides features for automatically checking the
specified validity of user inputs.
These controls are available in the System.Web.UI.WebControls
namespace.
d) Custom Controls
You can develop your own server controls by extending an existing
control or group of controls to provide additional functionalities.
There are two versions of custom controls:Web User Controls and
Web Custom Controls.
The Web User Controls are easy to develop, and are typically stored
as .ascx files.
The Web Custom Controls require in-depth knowledge of Object
Oriented Programming and the Common Language Runtime
(CLR).These are stored in compiled form as assemblies.
In an IIS environment, the ASP and ASP.NET can run side by side. If you
install ASP.NET, your existing ASP applications will continue running.
The IIS uses the ASP Engine to process the .asp files, whereas it uses the
ASP.NET Engine to process the .aspx files. Session states and
application states are not shared between ASP and ASP.NET pages.
Collecting Data Using HTML Forms
Page directives are used to set various attributes about a page.The ASP
Engine and the compiler follow these directives to prepare a page.
There are many kinds of directives.The most frequently ones are the
following: @ Page, @ Import,@ Implements, @ Register,@ OutputCache
and @ Assembly directives.
These directives can be placed anywhere in a page, however, these are
typically placed at the top.
Page Directives and Their Functions
a) @ Page
We may use this directive to declare many page-related attributes
about a particular page. For example, we use this directive to declare
the language to be used in a page, such as
<%@ Page Language=”VB” Debug=”true” %>
There are numerous attributes of this directive. Some of the
frequently used ones are these: AutoEventWireup, Buffer,
ClientTarget, EnableSessionState, ErrorPage, Debug, Trace,
TraceMode, and so on.
b) @ Import
We use this directive to import a namespace in the page class file.
<%@ Import Namespace=”System.Data.OleDb” %>.
c) @ OutputCache
We can use this directive to specify how to cache the page. In the
following example, we are setting the duration that a page or user
control is output cached:
<%@ OutputCache Duration=”10” /%>.
d) @ Register
This directive is used to register a custom control in a page. In the
following example, we are registering one of our user custom controls
in page:
<%@ Register tagprefix =”utoledo” tagname=”Time”
Src=”TimeUserControl.ascx”%>.
e) @ Assembly
We use this directive to link to an assembly to the current page or
user control. The following example shows how to link to an assembly-
named payroll:
<%@ Assembly Name=”Payroll” %>.
f) @ Implements
This directive enables us to implement an interface in our page. In the
following example, we are implementing the IpostBackEventHandler
interface in one of our user controls:
<%@ Implements
Interface=”System.Web.UI .IPostBackEventHandler” %>.
Code-Behind versus In-Page Coding
You can use the HtmlAchor control (<a>) to navigate from a page to
another page.
<a runat = "server" id = "programmaticID" href ="linkurl" name =
"bookmarkname"
OnServerClick = "onserverclickhandler"
target="linkedcontentframeorwindow"
title="titledisplayedbybrowser">
If necessary, we can use this control to dynamically modify the attributes
and properties of the <a> element and display hyperlinks from a data
source.
The href attribute contains the URL of the page to be linked to.
Using the HtmlTable Control
We may define rows using <tr> tags. Table cells are defined using <td>
tags.This control is a container control, and so we can embed other
controls in its cells.
It has the following attributes:
<table runat="server" id="programmaticID" align=left|center|right
bgcolor="bgcolor"
border = "borderwidthinpixels" bordercolor="bordercolor"
cellpadding = "spacingwithincellsinpixels"
Cellspacing = "spacingbetweencellsinpixels" height="tableheight"
rows="collectionofrows" width="tablewidth" >
</table>
Using HtmlInputText and HtmlTextArea Controls
You can use both of these controls to collect text data from the user.
You can use the HtmlInputText control to implement server-side code
against the HTML <input type=text> and <input type=password> tags.
Its major attributes are these: type (text or password), runat, id,
maxlength, size , and value.
The HtmlTextArea control enables the user to enter multi-line text. It is
the server-side equivalent to the HTML <textarea> element. Its rows
and cols properties can be used to define its size. You can use its
onserverchange attribute to run an event handling function.
Using HtmlButton and HtmlImage Controls
The HtmlInputButton supports the HTML Reset and Submit button types.
Using the HtmlInputFile Control
Label
A Label is used to display text. If we want to display static text, we do
not need a Label server control; we should instead use HTML. We
should use a Label server control only if we need to change its
properties via server code.
TextBox
A TextBox control enables the user to enter text. By default, the
TextMode property is SingleLine, but it can also be set to Multiline or
Password. In case of Multiline text box, the Rows property determines
the height. If its AutoPostBack property is set to True, it generates a
PostBack on its Text_Changed() event.
Button
Button controls can be placed inside other container controls, such as
DataList, DataGrid and Repeater.
LinkButton
The LinkButton renders a hyperlink in the page.
ImageButton
The ImageButton displays an image that responds to mouse clicks. We
can also use it as an image map. Thus, we may pinpoint where in the
graphic the user has clicked.
CheckBox
It enables the user to input Boolean data: true or false, yes or no. Its
Checked property can also be bound to a data field of a data source. Its
CheckedChanged() event can be used for AutoPostBack.
CheckBoxList / DropDownList / ListBox / RadioButtonList
These controls are derived from the ListControl abstract class.
All three types of buttons (Button, LinkButton and Image button) cause
PostBacks when the user clicks them.
HyperLink
It displays a link to another page. It is typically displayed as text
specified in its Text property. It can also be displayed as an image
specified in the ImageUrl property. If both the Text and ImageUrl
properties are set, the ImageUrl property is displayed. If the image
does not exist, then the text in the Text property is shown. Internet
Explorer uses the Text property to display ToolTip.
Image
We may use the Image control to display an image on the Web page.
The ImageUrl property specifies the path to the displayed image.
When the image does not exist, we can specify the text to display in
place of the image by setting the AlternateText property. The Image
control only displays an image. If we need to capture mouse clicks on
the image, we should instead use the ImageButton control.
Panel
This can be used as a container of other controls. This control is
rendered as an HTML <div> element.
RadioButton
It creates an individual radio button on the page. We can group them
to present mutually exclusive choices.
Table
It enables us an HTML table. A table can be built at design time with
static content, but the Table control is often built programmatically
with dynamic contents. Programmatic additions or modifications to a
table row or cell do not persist on PostBack. Changes to table rows or
cells must be reconstructed after each post to the server. In these
cases, better alternatives are DataList or DataGrid controls.
Xml
This control can be used to transform XML documents.
Using the ListControl Abstract Class
A number of basic Web controls have been derived from the ListControl
abstract class.These are CheckBoxList, DropDownList, ListBox, and
RadioButtonList.
We can set their AutoPostBack properties to true to trigger postbacks on
their SelectedIndexChanged events.
The Items(i).Selected property can be used to check if the user has
selected an item in the list and the Items(i).Text property enables us to
extract the text of the selected item.
Using HyperLink Controls
Suppose that we want the user to enter the phone number, date of birth,
hiredate, password, and confirmation of password in a specified format
therefore you can put constraint as follows:
The phone number must follow a pattern like (ddd)ddd-dddd for
employees in the USA. It should match dd.dd.dd.dd for employees in
France.
The date of birth must be between 1/1/1940 and 1/12/1985.
Hire date must be after the date of birth and before 6/15/2001.
The user should enter the password twice, and both entries must be
identical.
<asp:RangeValidator id="ranvDateHired" runat="server"
type="Date" display="dynamic"
controlToValidate="txtDateHired" errorMessage="Hire date
must be before 6/1/2001" minimumValue="1/1/1900"
maximumValue="6/15/2001" >
</asp:RangeValidator>
<body><h2><center>Cathy's E-Shop</h2>
<asp:Repeater id="repeater1" runat="server" >
<HeaderTemplate><table></HeaderTemplate>
<ItemTemplate><tr>
<td><asp:Image height=100 width=100 Img src='<%#
Container.DataItem("ImagePath")%>'
runat="server"/>
</td>
<td>Product ID:
<%# Container.DataItem("ProductId")%><br>
Description: <b><i>
<%# Container.DataItem("ProductName")%></b><i><br>
<b>Unit Price:
<%#
FormatCurrency(Container.DataItem("Price"))%></b><br>
</td></tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</center>
</body>
Using Event Bubbling and Capturing Events in a Repeater Control