(How)
C^# You Are - 21 January 2007
- What is the difference between the DataSource and DataSourceID
properties of an ASP.NET data bound control? Answer
The DataSource property is of type Object and
contains the data to be bound to the control. This property is normally set
to an array or collection of data to be displayed. In v1.x this property was
used to assign data to controls. After assigning a value to this property
the DataBind method is called to bind the data to the control.
The DataSourceID property contains the identifier of the control
that contains the items to be bound to the control. Normally this identifier
matches a DataSource object contained on the page. Unlike
the DataSource property, setting this property causes the DataBind
method to be invoked automatically in most cases through an intermediary property.
Therefore, in general, it is not necessary to call DataBind.
- What is the difference between the StyleSheetTheme and
Theme properties of an ASP.NET page? Answer
Firstly, for clarification, a theme is nothing more than a collection of control
types and their attribute values (known as a skin). For example a theme can
indicate that all labels are displayed in bold text or that all grids have a pager.
Most properties of a control can be set through a theme. Themes can also be
applied to specific instances of types through a skin identifier. This is
useful for creating custom categories of types. For example we might create
a custom skin that says all labels with a skin identifier of ErrorMessage
use a foreground color of red.
The Theme property specifies the theme to use for the page.
The theme skins override any control settings defined on the page. In the
page life cycle you can envision the theme being applied after the OnInit
method/event of the page is raised. Therefore the theme is applied after the
page has been initialized and the control properties loaded from the ASPX page.
The theme overrides any attributes defined in the ASPX page if a conflict should
happen to occur.
The StyleSheetTheme property also specifies the theme to use for
the page.
However the style sheet theme works like a stylesheet class.
Any skins are applied after the controls are created but before their attributes
are initialized from the ASPX page. MS introduced the new OnPreInit
method/event to v2 in order to support this. OnPreInit is
called after the controls of the page are created but before their attributes are
set as defined in the ASPX page. Therefore the theme provides the basic settings
of the control while each page can change the default.
- What is a build provider in ASP.NET? Answer
A build provider, added in v2, is a provider that takes a specific type of file
and compiles it for consumption in ASP.NET. Prior to v2 ASP.NET understood
specific file types implicitly. To make ASP.NET more extensible build providers
were introduced to remove any implicit file type mapping. In v2 a build provider
can be used to compile XSD files into consumable data set classes or WSDL files
into web service proxies.
You can define your own custom build providers to handle custom file types.
For example you might define a custom file type called .bus that contains
a declarative definition for a business object. When ASP.NET runs across this
file it will dynamically compile it into a new class that can be used. Defining
your own custom build providers can be a little daunting so use them sparingly.
- What is an expression builder in ASP.NET? Answer
Another new feature in v2, an expression builder is a custom class that allows you to define expressions inside an ASPX page. Expressions in ASPX pages are identified
by the <%$ expressionType: expression %> syntax. expressionType
identifies the type of the expression while expression is the expression
to evaluate. ASP.NET ships with several expression builders already such as
ConnectionStrings, AppSettings and Resources.
These expression builders use the expression to identify the connection string,
app setting or resource to retrieve.
The cool thing about expression builders is that you can create your own.
For example you might create a Version expression builder that can retrieve
the various portions of an assembly version or a Database expression builder
that can execute SQL code and load a value from the database. You create new
expression builders by deriving from System.Web.Compilation.ExpressionBuilder.
You must then register the expression builder in the config file under the compilation\expressionBuilders
element.
- What does the ThemableAttribute do in ASP.NET? Answer
By default in v2 all properties of a control are themable meaning that the value
of the property can be specified by a theme. Many properties, such as any
that are specific to a control instance like the data binding properties or the
unique identifier of the control make no sense in a theme file. The ThemableAttribute
attribute, applied to properties, specifies that a property can not be set in a theme. Data
binding properties would normally have this attribute. Attempting to set the
property within a theme will cause an error.
- What does the DesignerSerializationVisibilityAttribute do in ASP.NET? Answer
This attribute, applied to properties, specifies how the property of a server control
are serialized. By default the property is serialized to the ASPX file as
an attribute. In general this works well.
Some properties should not be serialized at all. In this case you can specify
the DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)
to prevent the property from being serialized. This is normally set for properties
that have no design-time value such as the count of the number of rows in a grid.
Normally this attribute setting is used in combination with the BrowsableAttribute
to hide the property at design time.
Sometimes a property is simply used as a collection of other properties. In
this case the property itself is not important but what it contains is. The
DesignerSerializationVisiblity.Contents value can be used with
DesignerSerializationVisibility to serialize only the contents
of the property rather than the property itself. This variant is not used
as often.
- What does the PersistenceModeAttribute do in ASP.NET? Answer
This attribute is similar to DesignerSerializationVisibilityAttribute
in that it is applied to the properties of a server control and determines how the
property is serialized. By default properties are rendered as attributes.
For simple types this is fine but for complex types or child classes this does not
work well. The persistence mode tells ASP.NET how to serialize the property.
Values include as an attribute, as a child element or as an HTML string.
- How do you get ASP.NET to display exception information in a web site when an unhandled
exception occurs? Answer
By default unhandled exceptions in a web page will cause the exception information
to be displayed only when the browser is running on the same machine as the server.
In all other cases a generic error page is displayed. By using the mode
attribute of the customErrors element in the web.config you can
configure this option.
Setting the mode to Off displays full exception information irrelevant
of where the browser is calling from. When the debug option is set to
true for compilation a full exception log and stack trace will be generated.
Setting the mode to On will always displays a generic error message.
Setting the mode to remoteOnly, the default, will display a generic
message while running outside the server and detailed exception information while
running on the server.
In a production environment the mode should be set to either On
or remoteOnly. In a debug environment the mode can be set
to Off to allow easier debugging but should be changed before sending
to production.
- What is the difference between a server control and an HTML control? Answer
The big difference between the two is whether ASP.NET can interact with the control
or not. Server controls can be programmatically accessed while HTML controls
can not. HTML controls exist within the ASPX page but are not directly accessible
to ASP.NET. They are rendered directly to the output stream. Server
controls have the runat="server" attribute on them. Server
controls are great for creating dynamic websites but they have a higher server cost
both in terms of memory and CPU usage. HTML controls have almost no overhead
since they are simply rendered to the output. However HTML controls must be
completely controlled through client-side code. HTML controls are generally
used to control the non-interactive portion of a website such as labels, links and
positions (through tables and DIVs).
Be aware that ASP.NET has a collection of server controls known as the HtmlControl
controls. These controls represent server control versions of the HTML controls.
For example there is an HtmlTable server control that renders as
an HTML control but acts like a server control. This control is created when
you apply the runat attribute to a <TABLE>
element. In general these controls are not used that often as the ASP.NET
server controls provide more flexibility but they can be useful when you need to
programmatically render HTML controls (such as in a server control).
- Name the commonly used state management objects in ASP.NET? Answer
Here are the most common state management objects.
- Application - A dictionary used to store application-level settings.
- Session - A dictionary used to store per-user settings.
- Profile - A new strongly-typed class used to store and persist user-level settings.
- Cache - A dictionary used to store application-level settinsg with builtin support
for memory restrictions, change detection and timeout values.
- Items - A per-request dictionary for storing information during a request.
- ViewState - A dictionary of key-value pairs storing per-page settings.
- Cookies - Contains cross-request information on the client and can be persisted
across site accesses for days.
|
|