ASP.NET is Control-Driven
If you've been working with ASP
code long enough to remember the days of the Design-time Controls that were
found in Visual InterDev, you might have a negative view of control-based web
development. Many ASP developers had problems working with the design time
controls found in InterDev. These controls wouldn't work as intended, and they
would rarely deploy well to other Windows/IIS servers.
With
ASP.NET, the idea of server-based controls has matured and expanded to include
much greater degrees of control, flexibility, and customization. Most controls
that can be used with ASP.NET require little code to be embedded onto ASPX
pages. In fact, many controls can be exposed to server functionality by simply
adding a few attributes to HTML elements. This section will introduce you to
the various types of controls useful in ASP.NET development.
These
controls are the epitome of encapsulation. Complex user interface design code
is hidden away behind the scenes, so that developers can make use of it with a
single line of code. The control can then be accessed programmatically to
customize its appearance, react to any events it raises, and even provide data
for it to display. The ASP.NET controls fall into three major groups.
Server-aware HTML
Controls
The quick and easy
way to start your migration path from ASP to ASP.NET is to add runat="server" to your HTML controls, thus converting them from standard controls
to server controls. Let's look at a quick example:
<input type="text"
id="mytext" runat="server"/>
The addition of this simple attribute enables us to
work with our HTML control programmatically. We can work with events that it
generates, set attributes, and bind controls to data sources. For example:
void Page_Load(object sender, EventArgs e)
{
mytext.value = "hello world" ;
}
HTML controls are quick and easy to implement, and they
have the same set of attributes that you're used to when working with standard
HTML elements on a page. We'll learn more about these controls in Chapter 4.
ASP: Controls
The
ASP.NET built-in controls are very similar to the HTML controls, except that
the attributes that each control supports are pretty much the same for each
control. When working with HTML controls, you'll find that the attributes that
each control supports are somewhat arbitrary, depending on the control. The
ASP: controls have a consistent interface that makes it very easy to get used
to using them. For example:
<asp:TextBox id="MyASPText" Text="Hello ASP.NET
TextBox" runat="server" />
<asp:CheckBox id="MyASPCheck" Text="My
CheckBox" runat="server" />
And
again we can work with these controls in a similar manner:
...
MyASPText.Text = "New text";
MyASPCheck.Text = "Check me!";
...
The
list of attributes that each control supports is defined in a base class: System.Web.UI.WebControls.WebControl. We'll talk more about the attributes these controls support, and
how we work with our controls programmatically, in Chapter 4.
Data-bound Controls
One of the
coolest features of ASP.NET controls is the ability to bind data to our
controls. We can access a data source, read in some data and store it in either
a DataReader or DataSet object, and bind the data to a specific control, for example, the ASP:DataGrid control. With barely any code we are able to display all the data
in a table. Let's take a quick look:
In the HTML section of our ASP.NET page, we include
the following:
<asp:DataGrid
id="MyDataGrid" Runat="Server"/>
Then either in a script block, or in a separate
code-behind class, we have some code looking a bit like the following:
...
//Connect to data source and place the data in a DataReader object
...
MyDataGrid.DataSource = MyDataReader;
MyDataGrid.DataBind();
The most basic implementation of this can look like
the following:
On top of this basic representation, we're able to
define values for things like font styling, background colors, header styles,
and much more, so our data grid can become even more user-friendly. We'll find
out a lot more about data binding and the DataGrid control in Chapter 5.
Also in that chapter we'll learn about many more controls that can be bound to data,
including many different types of list controls.
On top of the built-in controls, we can create our own
controls for elements of functionality that we'd like to reuse. We'll look at
this topic in more detail in Chapter 4.
Buy Fast Track ASP.NET here
© Copyright 2002 Wrox Press
These chapter is written by Brady Gaster, Marco Bellinaso & Kevin Hoffman
and taken from "Fast Track ASP.NET" published by Wrox Press Limited in June 2002; ISBN 1861007191; copyright © Wrox Press Limited 2002; all rights reserved.
No part of these chapters may be reproduced, stored in a retrieval system or transmitted in any form or by any means -- electronic, electrostatic, mechanical, photocopying, recording or otherwise -- without the prior written permission of the publisher, except in the case of brief quotations embodied in critical articles or reviews.
|
|