(How)
C^# You Are - 17 June 2007
This week's questions revolve around ASP.NET.
- What is the difference between a HyperLink and a LinkButton? Answer
Both controls look like a standard web link. When a HyperLink
is clicked the user is immediately directed to a new URL. A LinkButton
is used to expose standard button-style functionality but with the web
link-style look. When a LinkButton is clicked the page posts
back to ASP.NET. Normally the postback occurs to the page containing the button
but another page can be specified instead. Any page validation occurs before
the postback. A HyperLink is a standard web link so no postback
occurs nor do any validators get called.
- What is the difference between an Image and an ImageButton? Answer
Both controls display an image. The Image control is a standard
HTML image. The ImageButton control is a standard ASP.NET
button that uses an image rather than text. When the ImageButton
is clicked a postback occurs. An Image does not normally
respond to any interaction but hyperlinks or poup-text are often associated with
them anyway.
- What is the difference between a DataGridView and a GridView? Answer
A DataGridView is a WinForms grid used to display rows of data
with multiple columns. A GridView is the ASP.NET equivalent.
They both perform similar functions although the underlying details are quite a
bit different. Prior to v2 the DataGrid was used for both
WinForms and ASP.NET apps. They were two separate controls with the same name.
Since they behaved so differently it caused a lot of confusion when someone would
ask a question about the DataGrid. Hence in v2 the controls
have different names.
Although the controls look and act similar they are quite a bit different.
The DataGridView contains columns deriving from DataGridViewColumn
whereas a GridView has columns deriving from DataControlField.
Perhaps a bad name but here to stay nonetheless. Rows in a GridView
derive from GridViewRow wherease DataGridView
rows deriving from DataGridViewRow. Only MS knows why
DataControlField was used instead of GridViewColumn.
There are quite a few other differences as well. When asking questions about
either of these controls make it clear which one you are using.
- What is the ObjectDataSource used for? Answer
This data source is used to bind custom business objects to bindable controls such
as the GridView. As with all other data sources this class
provides a level of isolation between the data to be displayed and the displaying
control. If you were talking to a SQL Server database then you'd use a
SqlDataSource. The ObjectDataSource is great
for wrapping static method calls of managerial classes such that business objects
can be used in ASP.NET controls. Unfortunately there are some limitations
on the data source since it does not have acces to the underlying data directly.
- What format(s) can a sitemap be stored in? Answer
A sitemap provides a map of the site layout for use in navigation controls.
Site map providers are used to convert the site layout into a consistent format
usable with navigation controls. Out of the box .NET supplies only a single
provider: XmlSiteMapProvider. This provider is really limited
as it talks to a fixed named XML file. One has to wonder why something so
trivial (in relation to other areas of .NET) was so poorly supported in .NET.
At a minimum database support should have been included. Fortunately it is
relatively straightforward to write your own provider. In fact there are several
versions online to talk to a SQL database. Here is a link to one published
in MSDN Magazine.
- What is the purpose of a validation group? Answer
One of the big limitations of ASP.NET validators in v1.x was the inability to programmatically
enable and disable validators on the client side. It is often the case where
different areas of a page are enabled or disabled based upon selections within the
page. To avoid postbacks it is necessary to disable validation when the target
control is disabled. Validation groups allow for this.
A validation group is nothing more than a name for a group of validators.
When a submit is about to occur (such as when clicking a button) the control is
examined to see if it has an associated validation group (it exposes a ValidationGroup
property). If it does then only those validators in the same group are validated.
This allows for areas of a page to be associated with a group. Only when the
button associated with the group is clicked when the group be validated. It
is not a perfect solution but it works.
- How do you prevent a cancel button from causing validation? Answer
The CausesValidation property (not to be confused with the WinForms
property) is associated with any control that can cause a submit to be posted.
When the property is set to false then the submit will not cause any validators
to run. Normally it is set to true. When the submit occurs the validators
are run on the client to avoid a potentially useless postback. Validation
also occurs on the server.
To implement a cancel button drop a normal button onto the form and then set the
CausesValidation property to false. The button will then
submit back to the page without requiring any validation.
- What is the difference between Session and Profile? Answer
Both objects perform similar functions and, in fact, are implemented through the
same mechanism. Both objects are per-user. Session
is implemented as a string dictionary that holds objects. When the user's
session expires the information is lost. Profile is implemented
as a dictionary as well but the compiler/IDE generates a strongly-typed class deriving
from ProfileBase that exposes each of the entries as a strongly-typed
field. Since the class has to be generated during compilation the layout (fields)
of the class must be defined at compile time. This is done by adding a
<profile> element to the web.config and defining each of the
profile properties, their type and some optional additional values. During
compilation the element is converted to strongly typed class and exposed through
the Profile property of the page.
As if strongly typed properties isn't enough to make Profile sound
like a better option the other benefit is that the properties can be persisted across
sessions. In this case the profile data is (by default) stored in a SQL server
database. When the user returns to the site the profile information is restored.
Session never works this way. Of course not every value needed for a user
should be stored in Profile as this would space. In general
reproducible values that are specific to the user should be stored in Session.
User preferences should be stored in Profile. Preferences
might include the time zone of the user, the preferred theme or their preferred
title.
- How do you enforce validation of a field that must contain an e-mail address? Answer
To enforce the e-mail address you would use a RegularExpressionValidator
applied to the text field. However what might surprise you is that irrelevant
of the expression an empty string is always valid. In all but one case an
empty string is valid in all validators. The only validator that does not
consider an empty string to be valid is the RequiredFieldValidator.
Therefore to verify that an input value is not only in the correct format but also
that it was specified at all you must apply two validators; RequiredFieldValidator
and RegularExpressionValidator.
- What does the EnableViewState property on controls do? Answer
This property, normally set to true, determines whether the control stores information
in view state or not. Viewstate is used to hold things like the initial value
for a text box, the URL of a hyperlink and other properties. When a page is
rendered the first time this information is determined by the ASPX page (through
the control attributes) and programmatically. On postbacks the data is initialized
through the ASPX page attributes but then overridden by any view state data.
Since almost all properies are set through the view state it is redundant but necessary
in order to capture property values across postbacks.
Sometimes it is not beneficial to remember a control's value across postbacks.
For example a label that is used to display messages might be invisible initially,
perhaps changing color and font when displaying an error message. The default
value (as defined in the ASPX page) would be the desired settings whenever the page
begins to render. Programmatically setting the properties based on the state
of the page would be the preferred approach. Rather than resetting the control
each time the EnableViewState can be set to false. Now each
time the page is rendered it remains at whatever state the ASPX page set it at.
EnableViewState is also useful in other cases. If a grid
(which will eat up a lot of view state space) is always repopulated when the page
is rendered then there is no benefit in saving the grid contents across postback
(unless the grid allows for interaction). Setting EnableViewState
to false will save on rendering time and download size. Be aware however that
disabling view state for a control will effectively disable any interaction with
the control on postback as it will always return to its previous state thus controls
like buttons or fields are effectively useless.