Lesson 1: Using Controls
This lesson really begins to examine ASP.NET 1.X technology. Controls are the centerpiece of this technology. The first sentence of this lesson says, "Controls are the tools for all the tasks you perform on a Web form."
There are two ways to arrange controls on a Web form, Grid layout and Flow layout. There are two types of controls, Server controls and HTML controls.
- "Because HTML controls have a one-two-one correspondence with standard HTML elements, they provide more direct control over what appears on a page."
HTML controls are more efficient than Server controls when using data binding. "...it's more efficient not to maintain state information for bound controls." This reinforces the opinion here that HTML controls should have precedence over Server controls in Web forms design.
- "Server controls provide overlapping functionality."
For some reason, the Literal control corresponds with the Horizontal Rule HTML control in Table 4-2. The opinion here is that Literal control along with the Placeholder control are the two most useful Server controls. This begs the question, 'Why is the Literal control omitted from the "Working with Text" section?'
- "Use the ListBox, DropDownList, and Table controls for simple dynamic tables and lists. Use the DataGrid, DataList, and Repeater controls for complex tables and lists that contain other controls or are bound to data."
Add static items to the ListBox, DropDownList and Table controls with the Collection Editor from the Properties pane at design time. Add items at runtime via the Add() method of the Items collection of the ListBox and DropDownList controls---and the Add() methods of the Rows and Cells collections of a Table control.
- "The Table control, however, will automatically store data only for the table cells created at design time in the Collection Editor."
This is the first example in the book that demonstrates the limitations of ViewState because, evidently, the Table control Rows and Cells collections cannot be serialized and stored between requests (probably for performance reasons). In fact, the RebuildTable() routine in the code samples is a design pattern for manually manipulating ViewState to maintain state for controls that are not adequately supported.
- "Use the SelectedItem property to get the current selection from a list."
When an Item is not selected, the SelectedItem property returns null (or Nothing in VB.NET).
- "Controls can get their values from any data source in your application. Data sources can be any public data."
At design time data bindings can be defined in the DataBindings dialog box. The Custom binding expression option is used quite frequently and should naturally lead to the subject "Data Binding Expression Syntax" covered here:
http://msdn.microsoft.com/
library/en-us/cpgenref/html/
cpconDatabindingExpressionSyntax.asp
This in turn will lead to inline blocks like this:
<asp:DropDownList id="DropDownList1" runat="server" DataSource="<%# arrData %>">
</asp:DropDownList>
where the delcared attribute DataSource="<%# arrData %>" is rendered in HTML like this:
<select name="DropDownList1" id="DropDownList1">
<option value="This">This</option>
<option value="that">that</option>
<option value="and">and</option>
<option value="the">the</option>
<option value="other">other</option>
</select>
Keep in mind that the same declaration can cause this to render:
<select name="DropDownList1" id="DropDownList1">
</select>
This happens when the DataBind() method is not called before the Web form loads.
- "When you use data binding with a server control, you can turn off state management for that control."
This implies that using data binding and ViewState for a control reduces performance and can be redundant. However, in the following lines of code, SelectedIndex will always be -1 when ViewState is turned off:
if(this.IsPostBack)
{
int idx = this.DropDownList1.SelectedIndex;
this.DataBind();
this.DropDownList1.SelectedIndex = idx;
}
else
this.DataBind();
The conclusion can be turn ViewState off when using data bound controls to display reports but leave it on for round-tripping (required for many editing scenarios).
- "A template is a set of HTML elements or server controls, or both, that will be repeated for each data item in the control."
What resonates here is the wording, "a template is a declaration of HTML formatting" that will be repeated for each data item. This reminds me that Visual Studio saves my template "drawing" as ASP.NET tags in the HTML source. This is a superior alterative to the code auto-generated and hidden in a region in Windows forms applications.
- "The Button, LinkButton, and ImageButton server controls all trigger postback events to perform commands."
This is done through code-generating hookups to Javascript files installed by ASP.NET under a folder called \\aspnet_client.
- "Using the Button and LinkButton controls' Click event procedure is straightforward. The ImageButton control provides an additional capability."
Yes. And this capability is pretty cool! A server-side image map handler can be set up with the ImageButton control because System.Web.UI.ImageClickEventArgs has X and Y properties to capture mouse clicks. See:
http://msdn.microsoft.com/library/
en-us/cpgenref/html/cpconimagebuttonwebcontrol.asp
- "Use the RadioButton, RadioButtonList, Checkbox, or CheckboxList controls to get Boolean value settings from the user."
The RadioButtonList and CheckboxList controls solve the problem of toggling among a group of controls. Optionally, the RadioButton control has a GroupName property to allow toggling among a non-columnar list of radio buttons. The Checkbox control does not have such a property for grouping.
- "There are many ways to display graphics on a Web form..."
The Background property of a Web form, the Image, ImageButton and the XML-driven AdRotator controls handle displaying graphics.
- "Use the Panel control to group controls on a Web form."
This control solves the problem of hiding and showing groups of controls (as it renders as the HTML div element). It only supports Flow layout.
- "Use the Calendar control to get or display date information."
A pleasant surprise in this server control is its ability to select multiple dates. The following code sample run during the SelectionChanged event handles both single and multiple date selections:
if (calSource.SelectedDates.Count == 1)
// If one date is selected, display it.
lblDate.Text = "Selected date: " + calSource.SelectedDate;
else
// If multiple dates are selected, display them.
lblDate.Text = String.Format("Selected dates: {0} to {1}.",
calSource.SelectedDates[0],
calSource.SelectedDates[calSource.SelectedDates.Count - 1]);
- "The File Field HTML control is actually a Text Field control and a Submit Button HTML control bound together."
This is a composite HTML control---not a server control. Trying and catching a System.UnauthorizedAccessException exception is good design. The HttpPostedFile object of the HtmlInputFile.PostedFile property has a ContentLength property to check the size of a file before saving it to server disk.