Repeater应该不算是和DataGrid和DataList一路的,只不过看上去有一点像而已。
DataGrid/GridView主要是用来显示数据库报表类的数据的,或者说就是用来显示报表的。
DataList(我认为应该叫做ListView)可以用来显示“格子中自有天地”的数据。比如Blog的comments就可以用DataList来做。
A Brief Overview of the DataGrid Web Control
The DataGrid Web control was designed to display data in an HTML . Each row in the DataGrid's DataSource
is displayed as a row in the HTML . The DataGrid has an AutoGenerateColumns
property, which can be set to either True or False. If AutoGenerateColumns
is set to True (the default), each field in the DataSource
is displayed as a column in the resulting HTML . If, however, AutoGenerateColumns
is set to False, then the developer must explicitly specify what columns should appear.
One downside of a DataGrid is its rather "blocky" display. That is, the DataGrid displays each DataSource
record as a row in an HTML , and each field as a column in said table. While the DataGrid allows for the developer to customize the output for a particular column through the
, it still is restrictive in that each DataSource
record occupies precisely one HTML row.
Despite its limitations on specifying the display of the DataGrid's data, the DataGrid is the most commonly used data Web control due to its impressive feature list.
A Brief Overview of the DataList Web Control
The DataList Web control is useful for displaying data that can be highly customized in its layout. By default, the DataList displays its data in an HTML . However, unlike the DataGrid, with the DataList you can specify via the RepeatColumns
how many DataSource
records should appear per HTML row.
RepeatColumns="5" > Question: <%# DataBinder.Eval(Container.DataItem, "Description") %> Views: <%# DataBinder.Eval(Container.DataItem, "ViewCount", "{0:#,###}") %>
|
[View a Live Demo!] As the code example above shows, DataLists are comprised of a number of templates. Templates can contain a mix of HTML syntax and databinding expressions, as shown in the above ItemTemplate. Databinding expressions are ones delimited by <%#
and %>
, and contain code that's executed when the DataListItem's DataBind()
method is called. The ItemTemplate specifies the template used for each record in the DataSource
. The DataList contains other templates, which are listed below:
- AlternatingItemTemplate - if specified, each alternating item in
DataSource
uses this template instead of the ItemTemplate. - EditItemTemplate - the template used when a DataList row is in "edit mode".
- HeaderTemplate - the template for the DataList's header item. Precisely one header item is created for the DataList item if the DataList's
ShowHeader
property is set to True. - FooterTemplate - the template for the DataList's footer item. Precisely one footer item is created for the DataList item if the DataList's
ShowFooter
property is set to True. - SeparatorTemplate - this template, if specified, is applied after each DataListItem has been added.
The DataList is capable of performing sorting, paging, and editing of its data, but to do so requires quite a bit more programming than to accomplish these tasks with the DataGrid. Therefore, if you know you will be needing these functionalities, it is likely best to proceed with the DataGrid. If, however, you will not need these functionalities, but do necessarily need the extra control over the formatting, consider a DataList.
A Brief Overview of the Repeater Control
The Repeater control, unlike the DataGrid and DataList, is not derived from the WebControl
class, and therefore does not have the stylistic properties that are common to all Web controls. (These stylistic properties that are common to all Web controls include Font
, ForeColor
, BackColor
, BorderStyle
, and so on.)
The Repeater, like the DataList, only supports templates; however, the Repeater has only a subset of the DataList's template options. Specifically, the following templates can be used with the Repeater:
- AlternatingItemTemplate,
- ItemTemplate,
- HeaderTemplate,
- FooterTemplate, and
- SeparatorTemplate
The Repeater control provides the maximum amount of flexibility over the HTML produced. Whereas the DataGrid wraps the DataSource
contents in an HTML , and the DataList wraps the contents in either an HTML or
tags , the Repeater adds absolutely no HTML content other than what you explicitly specify in the templates.
Therefore, the Repeater is a good data Web control choice if you want to display data in, say, an unordered list. As the following code and live demo show, displaying database data in an unordered list using a Repeater is relatively straightforward. All you have to do is add the
tag to the HeaderTemplate, the
tag to the FooterTemplate, and an
tag along with the DataSource
field you want to display to the ItemTemplate:
<%# DataBinder.Eval(Container.DataItem, "Description")%>
|
[View a Live Demo!] The Repeater is a good choice if you need to display your data in a format different than an HTML . Unfortunately, the Repeater does not provide any built-in means for editing, sorting, or paging of data. However, these mechanisms could be added programmatically, but would result in a lot of code and effort.
Conclusion
This article examined the similarities and differences of the three data Web controls: the DataGrid, DataList, and Repeater. The main similarities between these three controls is in how they iterate through their DataSource
, building up a collection of DataWebControlNameItems. Furthermore, the three Web controls all share three events: ItemCreated
, ItemDataBound
, and ItemCommand
.
Each of the data Web controls has its own strengths and weaknesses. The DataGrid, for example, is great for quickly and easily displaying database data in an HTML , and allows for advanced features such as paging, sorting, and in-place editing to be added with little to no programming. However, the DataGrid is quite limited in the general format with which the data is presented. The DataList allows for more freedom. For example, in an earlier live demo we saw that using the DataList's RepeatColumns
property, multiple DataSource
records could be displayed in a single HTML row. Additionally, the DataList's content is specified via templates, which allows for a high degree of customization. The Repeater allows for the utmost flexibility in its output, since the only HTML rendered from a Repeater is the HTML generated in its templates. That is, no additional HTML output is generated, as is the case with both the DataGrid and DataList. The Repeater, however, does not have any built-in support for sorting, paging, or editing of its data.
Scenario : 判断给定的某年某月有多少天
Essentially, writing some code to determine the number of days in a given month of a given year with javascript is not the worlds most difficult task. The solution normally involves determining if the month is February, an month with 30 days or a month with 31 days, then (if February) checking if the year is a leap year. All these tests add up, however, and add several lines of code to your .js file.
They are unnecessary! Apparently, the javascript Date function allows you to overflow the day number parameter that you pass, creating a date in the next month. Deliberately overflowing the day parameter and checking how far the resulting date overlaps into the next month is a quick way to tell how many days there were in the queried month. Here is a function that does this:
function daysInMonth(iMonth, iYear) {
return 32 - new Date(iYear, iMonth, 32).getDate();
}
iMonth is zero based, so 0 represents January, 1 represents February, 2 represents March and 11 represents December.
iYear is not zero based, this is the actual calendar year number. (2006 is actually 2006)
How does this function work? It is quite simple. When the Date() function is given a day number that is greater than the number of days in the given month of the given year, it wraps the date into the next month. The getDate() function returns the day of the month, starting from the beginning of the month that the date is in. So, day 32 of March is considered to be day 1 of April. Subtracting 1 from 32 gives the correct number of days in March!
Pray that the browser developers know the correct way to determine whether a year is a leap year! (It's more complicated than a simple mod 4 == 0) Here is a quote from Wikipedia's page on leap years: "The Gregorian calendar, the current standard calendar in most of the world, adds a 29th day to February in all years evenly divisible by 4, except for centennial years (those ending in '00'), which receive the extra day only if they are evenly divisible by 400. Thus 1600, 2000 and 2400 are leap years but 1700, 1800, 1900 and 2100 are not."
ASP.NET includes controls and classes to simplify the process of adding login capabilities to your Web application.
The login controls include:
- Login 用户登录框
A user interface that prompts for user names and passwords and (Remember me 记住我) enables users to select whether they want to be automatically authenticated the next time they visit.
You can use the Login control with ASP.NET membership without writing any code, or you can write your own authentication code by adding a handler for the Authenticate event. - LoginStatus 用户登录按钮/链接 & 用户退出按钮/链接
Displays a login link for users who haven’t been authenticated and a logout link for users who are currently logged in. - LoginName 当前用户的username
Displays the current user’s user name, if logged in. - LoginView 登录后才可以看到的内容/链接
Enables you to display different information to users who are logged in.
For example, you could use this link to go to site features that are available only to authenticated users. - PasswordRecovery 找回密码
Enables password retrieval for a user by sending an e-mail message to the user or by having the user answer a security question. - ChangePassword 修改密码
Enables a user who is logged in to change his or her password. - CreateUserWizard 用户注册
Gathers information from a new user and creates a new account. - You can use a ValidationSummary control to display detailed error information provided by some of these controls.
Introduction
Microsoft ASP.NET AJAX enables you to quickly create Web pages that include a rich user experience with responsive and familiar user interface (UI) elements. ASP.NET AJAX provides client-script libraries that incorporate cross-browser ECMAScript (JavaScript) and dynamic HTML (DHTML) technologies, and it integrates them with the ASP.NET 2.0 server-based development platform. Using ASP.NET AJAX, you can improve both the user experience and the efficiency of your Web applications.
Why Use ASP.NET AJAX?
ASP.NET AJAX enables you to build rich Web applications that have many advantages over Web applications that are completely server-based. ASP.NET AJAX applications offer:
- Improved efficiency by performing significant parts of a Web page's processing in the browser.
- Familiar UI elements such as progress indicators, tooltips, and pop-up windows.
- Partial-page updates that refresh only the parts of the Web page that have been updated.
- Client integration with ASP.NET application services for forms authentication and user profiles.
- Integration of data from different sources through calls to Web services.
- A framework that simplifies customization of server controls to include client capabilities.
- Support for the most popular and commonly used browsers, including Microsoft Internet Explorer, Mozilla Firefox, and Apple Safari.
ASP.NET AJAX Architecture
ASP.NET AJAX consists of client-script libraries and of server components that are integrated to provide a robust development framework. In addition to ASP.NET AJAX, you can use the ASP.NET AJAX Control Toolkit and the community-supported features in the ASP.NET AJAX Futures releases.
ASP.NET AJAX Server Components
The ASP.NET AJAX server components consist of ASP.NET controls and components to manage the UI and flow of an application, and to manage serialization, validation, control extensibility, and so on. There are also ASP.NET Web services that enable you to access ASP.NET application services for forms authentication and user profiles.
ASP.NET AJAX Server Controls
The ASP.NET AJAX server controls consist of server and client code that integrate to produce AJAX-like behavior. The following list describes the most frequently used ASP.NET AJAX server controls.
- ScriptManager
Manages script resources for client components, partial-page rendering, localization, globalization, and custom user scripts. The ScriptManager control is required in order to use the UpdatePanel, UpdateProgress, and Timer controls. - UpdatePanel
Enables you to refresh selected parts of the page instead of refreshing the whole page by using a synchronous postback. - UpdateProgress
Provides status information about partial-page updates in UpdatePanel controls. - Timer
Performs postbacks at defined intervals. You can use the Timer control to post the whole page, or together with the UpdatePanel control to perform partial-page updates at a defined interval.
ASP.NET AJAX Web Services
ASP.NET AJAX exposes ASP.NET application services for forms authentication and user profiles to client script by using Web services. This enables you to help protect resources by using forms authentication and to persist user-specific settings on the server by using client script. In addition, ASP.NET AJAX includes network components that make it easy to return results from any Web service call.
For information and examples, see Asynchronous Communication Layer Overview and ASP.NET Application Services Tutorials.
ASP.NET AJAX Server Control Extensibility
ASP.NET AJAX enables you to create custom ASP.NET AJAX server controls that include client behaviors. For information and examples, see the ASP.NET AJAX Extensibility tutorials and the Microsoft ASP.NET AJAX Control Toolkit.
ASP.NET AJAX Client Components
The ASP.NET AJAX client-script libraries consist of JavaScript (.js) files that provide features for object-oriented development. This has not been previously available to JavaScript developers. The object-oriented features included in the ASP.NET AJAX client-script libraries enable a new level of consistency and modularity in client scripting. The following layers are included in the ASP.NET AJAX script libraries:
- A browser compatibility layer.
This provides compatibility across the most frequently used browsers (including Microsoft Internet Explorer, Mozilla Firefox, and Apple Safari) for your ASP.NET AJAX scripts. - ASP.NET AJAX core services,
which include extensions to JavaScript, such as classes, namespaces, event handling, inheritance, data types, and object serialization. - An ASP.NET AJAX base class library,
which includes components such as string builders and extended error handling. - A networking layer
that handles communication with Web-based services and applications, and that manages asynchronous remote method calls.
ASP.NET AJAX Control Toolkit
The ASP.NET AJAX Control Toolkit is a collection of samples and components that show you some of the experiences you can create with rich client ASP.NET AJAX controls and extenders. The Control Toolkit provides both samples and a powerful SDK to simplify creating and reusing your custom controls and extenders.
ASP.NET AJAX Community-supported Futures Releases
The ASP.NET AJAX community-supported Futures releases provide features that extend the core ASP.NET AJAX platform with functionality that remains under development and is not included in the Microsoft ASP.NET AJAX release. This includes additional extender controls, support for client declarative syntax (xml-script), and more.
Introduction
Partial-page rendering removes the need for the whole page to be refreshed as the result of a postback. Instead, only individual regions of the page that have changed are updated. As a result, users do not see the whole page reload with every postback, which makes user interaction with the Web page more seamless. Microsoft ASP.NET 2.0 AJAX Extensions enables you to add partial-page rendering to new or existing ASP.NET Web pages without writing client script.
Scenarios
ASP.NET 2.0 AJAX Extensions enables you to extend existing ASP.NET 2.0 applications and develop new ones that incorporate AJAX (Asynchronous JavaScript and XML) functionality. Use ASP.NET 2.0 AJAX Extensions when you want to do the following:
- Improve the user experience with Web pages that are richer, that are more responsive to user actions, and that behave like traditional client applications.
- Reduce full-page refreshes and avoid page flicker.
- Enable cross-browser compatibility without writing client script.
- Perform AJAX-style client/server communication without writing client script.
- Use the controls and components from the ASP.NET AJAX Control Toolkit.
- Develop custom Microsoft ASP.NET AJAX controls.
Partial-Page Rendering Features
Partial-page rendering relies on server controls in ASP.NET 2.0 AJAX Extensions and on client functions in the Microsoft AJAX Library. You do not have to use the Microsoft AJAX Library to enable partial-page rendering, because this functionality is provided automatically when you use the ASP.NET 2.0 AJAX Extensions server controls.
The primary features of Microsoft ASP.NET AJAX partial-page rendering include the following:
- A declarative model that works like ASP.NET server controls. In many scenarios, you can specify partial-page rendering using only declarative markup.
- Server controls that perform the underlying tasks required for partial-page updates. These include the ScriptManager control and the UpdatePanel control.
- Integration between ASP.NET 2.0 AJAX Extensions server controls and the Microsoft AJAX Library for common tasks. These tasks include enabling users to cancel a postback, displaying custom progress messages during an asynchronous postback, and determining how multiple clicks are processed.
- Error-handling options for partial-page rendering, which enable you to customize how errors are displayed in the browser.
- Cross browser compatibility, which is built into the Microsoft AJAX Library. Simply using the server controls automatically invokes the correct browser functionality.
Server Controls for Partial-Page Updates
To add AJAX functionality to ASP.NET Web pages, you identify individual sections of the page that you want to update. You then put the content of these sections into UpdatePanel controls. The contents of an UpdatePanel control can be HTML or other ASP.NET controls. You can add an UpdatePanel control to the page as you would any other control.
By default, postbacks originating from controls inside the update panel (child controls) automatically initiate asynchronous postbacks and cause a partial-page update. You can also specify that controls outside the UpdatePanel cause an asynchronous postback and that they refresh the UpdatePanel control's content. A control that causes an asynchronous postback is referred to as a trigger.
An asynchronous postback behaves much like a synchronous postback. All the server page life-cycle events occur and view state and form data are preserved. However, in the rendering phase, only the contents of theUpdatePanel control are sent to the browser. The rest of the page remains unchanged.
To support partial-page rendering, you must put a ScriptManager control on the page.
The ScriptManager control:
- keeps track of all the update panels on the page and their triggers.
- coordinates partial-page rendering behavior on the server.
- determines which sections of the page to render as a result of an asynchronous postback.
Enabling Partial-Page Rendering Support
You enable or disable partial-page rendering for a page by setting the EnablePartialRendering property of the ScriptManager control.
You can also specify whether partial-page rendering is supported for a page by setting the SupportsPartialRendering property of the ScriptManager control.
If you do not set the SupportsPartialRendering property and if the EnablePartialRendering property is true (which is the default), the capabilities of the browser are used to determine whether partial-rendering is supported.
If partial-page rendering is not enabled for a page, if it has been disabled, or if it is not supported in a browser, the page uses fallback behavior. Actions that ordinarily would perform an asynchronous postback instead perform a synchronous postback and update the whole page. Any UpdatePanel controls on the page are ignored, and their contents are rendered as if they were not inside an UpdatePanel control.
A control adapter is an optional class that, if present and properly configured, is used to render the Web control instead of using the control's default rendering logic. In short, using control adapters you can take the core functionality of a Web control, but completely customize the markup emitted.
This set of free control adapters, released by Microsoft, provide a set of control adapters that render a variety of built-in ASP.NET controls using preferred CSS techniques.
Moreover, the CSS control adapters ignore any control-level style settings that would get rendered as inline style elements in the rendered markup and instead require that style information be separated out and specified via CSS classes.
While these could be created from scratch, Microsoft has provided the ASP.NET 2.0 CSS Friendly Control Adapters that can be downloaded and plugged into your ASP.NET web applications with minimal effort. Once installed, the control adapters will no longer render inline style attributes (you need to use CSS classes) and replaces the
layouts of the Menu, TreeView, and other controls with CSS techniques.
The presentation settings for these classes can be specified in the page's style element or, ideally, in a separate CSS file.
Indeed, you can modify any ASP.NET control so it produces exactly the HTML you want.
I think Microsoft will build the CSS features into ASP.NET 3.0 as an essence. We won't use inline style anymore, neither table tags for layout, at least I won't. :-)
Actually, I really do not care about what kind of code is beneith the screen/page, I only require it works well as I and users expected. CSS or inline style, table or div, I don't give a fuck. As we get the better one freely and effortlessly, why not better. ;-) That's it.