Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
ASP .NET Database Programming Weekend Crash Course - J. Butler, T. Caudill.pdf
Скачиваний:
31
Добавлен:
24.05.2014
Размер:
3.32 Mб
Скачать

84

Saturday Morning

List Controls

List controls, like intrinsic controls, map closely to HTML elements. The reason they are in their own category is that List controls present the user with a list of options. The general rule here is that there is a parent object that contains multiple child objects. For example a DropDownList control contains one or many List items. The List controls are:

DataGrid

DataList

CheckBoxList

DropDownList

ListBox

RadioButtonList

Note

Since the use of Web controls is uniform regardless of type, I won’t go into too much detail here. We recommend that you look at your .NET documentation to get a complete listing of each List control’s properties, methods, and events.

The following snippet of code illustrates how to create a DropDownList by manually inserting a List Object:

<html>

<head>

</head>

<body>

<form runat=”server”>

<asp:DropDownList id=”cmbPeople” runat=”server”> <asp:ListItem value=”0” text=””/> <asp:ListItem value=”1” text=”Bill Gates”/> <asp:ListItem value=”2” text=”Larry Ellison”/> <asp:ListItem value=”3” text=”Steve Case”/>

</asp:DropDownList>

<br/><br/>

<asp:Button id=”Button1” Text=”Submit” runat=”server”/> </form>

</body>

</html>

This is a simple example, but probably requires a little clarification. First, you’ll notice that we used the following syntax to declare a ListItem:

<asp:ListItem value=”1” text=”Bill Gates”/>

Session 9—Using Web Controls

85

We could have just as easily used:

<asp:ListItem value=”1”>Bill Gates</asp:ListItem>

OK, now let’s try it programmatically:

<script language=”vb” runat=”server”>

Sub Page_Load(Sender As Object, e As EventArgs) cmbPeople.Items.Add(“”) cmbPeople.Items.Add(“Bill Gates”) cmbPeople.Items.Add(“Larry Ellison”) cmbPeople.Items.Add(“Steve Case”)

End Sub </script> <html> <head> </head> <body>

<form runat=”server”>

<asp:DropDownList id=”cmbPeople” runat=”server”/> <br/><br/>

<asp:Button id=”Button1” Text=”Submit” runat=”server”/> </form>

</body>

</html>

List controls can be bound to a data source very easily. We’ll introduce data binding in Session 22, “Introducing Data Binding.”

Cross-Ref

Rich Controls

Rich controls are very different from intrinsic and List Web controls. Intrinsic and List controls can roughly be traced to a single HTML element. Rich controls provide a piece of functionality that requires the use of multiple HTML elements. The ASP.NET rich controls are

AdRotator

Calendar

In the “old ASP world,” developers would have either (1) written a lot of HTML/ASP code or (2) written an ActiveX control to provide the functionality that is now provided by ASP.NET rich controls. The nice thing is that all state maintenance is managed for us and the user doesn’t need to download a component. Very convenient! There should soon be quite an aftermarket for custom rich controls.

86

Saturday Morning

Listing 9-4 demonstrates the ease-of-use of the Calendar control.

Listing 9-4 Using the Calendar control

<html>

<head>

<script language=”VB” runat=”server”>

Sub Calendar_Change(Source As Object, E As EventArgs) If Page.IsPostBack Then

lblMessage.Text = “You selected “ & ctlCalendar.SelectedDate.ToLongDateString()

End If End Sub </script>

</head>

<body>

<form id=”frmCalendar” runat=”server”> <asp:Label id=”lblMessage” runat=”server” /> <br/><br/>

<asp:Calendar id=”ctlCalendar” BackColor=”white”

BorderWidth=”3”

BorderStyle=”Solid”

BorderColor=”Black”

CellSpacing=”2”

CellPadding=”2”

ShowGridLines=”True” TitleStyle-BackColor=”white” TitleStyle-ForeColor=”black” DayHeaderStyle-ForeColor=”white” DayStyle-ForeColor=”black” SelectedDayStyle-BackColor=”red” OnSelectionChanged=”Calendar_Change”

runat=”server” /> </form>

</body>

</html>

If you view the source for output, you’ll see that Microsoft has really done us a favor by providing rich controls. The great thing about rich controls is that they are infinitely customizable.

Cross-Ref

The fourth category of HTML controls, Validation controls, deserves a session to itself. So, we’ll be discussing those in Session 11, “Validating User Input.”

Session 9—Using Web Controls

87

REVIEW

Web controls are server-side ASP.NET controls that render browser-specific HTML Web controls not only reduce the time required to develop HTML that will correctly render on different browsers, but Web controls can be used to maintain state between server requests. Web controls also allow us, as developers, to capture client-side events and process them on the server.

QUIZ YOURSELF

1.What advantages do Web controls provide over HTML controls? (See session introduction.)

2.What are the four basic categories of Web controls? (See session introduction.)

3.(True/False) Web controls map one-to-one with HTML elements. (See session introduction.)

S E S S I O N

10

Introducing User Controls

Session Checklist

Understanding the importance of User Controls in the .NET Framework

Learning to create a User Control

After working through the previous two sessions, you should have a good grasp on the controls, HTML and Web, which are provided with ASP.NET. Although the controls packaged with ASP.NET are very useful, chances are you will run into a situation

where creating a custom control might be a good idea. Why? First, the standard ASP.NET controls are developed to meet the most common functional requirements; they were not designed to meet every requirement, or very specific requirements. If an HTML or Web control meets one of your functional requirements but only in a general way, you could end up writing a ton of code in your ASP.NET pages to customize it. Furthermore, if that functionality is required on multiple pages (which from our experience is quite common), maintenance could turn out be a major headache. Second, your pages could require the combination of several ASP.NET controls. Again, if you code this functionality into each ASP.NET page, you’re really shooting yourself in the foot. Maintainability! Maintainability! Maintainability!

Both of these common situations can be addressed with the use of User Controls. User Controls provide an easy way to partition and reuse simple, common user interface (UI) functionality across a Web application. Furthermore, User Controls are compiled on demand and cached in server memory so you can gain a bit of a performance boost. User Controls do not need to be authored in the same language as the ASP.NET page in which they are being included. For example, if one developer is creating an ASP.NET using Visual Basic,

he or she can include a User Control written in C# or C++. From a business perspective, allowing developers to code in the language with which they’re most fluent can drastically improve performance. Plus, your resource pool broadens.