Asp. NET provides a template-based layout for display and curd database operations, making it an excellent way to build data-centric Web applications.
When you write a user-centric application, you always need some form of data set, at least, you need to retrieve data from a data source such as a relational database or an XML file, and format it before displaying it to the user. Despite the fact that previous versions of ASP.net provide data-centric display controls such as the GridView, which lack the customizable and scalable features required by professional Web developers, asp.net3.5 provides a new control ListView to address this problem, providing a very good customization and extensibility , using these features, you can display data in any format, use templates and styles, and perform curd (create, read, update, delete) operations with minimal code. This article focuses on data access steps when using the new ListView control, and also includes advanced features such as editing data and handling events.
Getting Started with ListView controls
Asp. NET provides most data-bound controls that use additional markup to automatically encapsulate display data, for example, the GridView control displays its data in an HTML table (<table>), one row (<tr>) for each record, Each field is displayed as a cell (<td>), although you can customize the appearance of the GridView using the TemplateField component, but the output of the GridView is still limited to a table component. But sometimes you want to completely control the appearance of HTML tags produced by data-bound controls, which is the advantage of the ListView control, which is not using additional markup to encapsulate its output, but rather the exact HTML description you specify, You can specify precise markup by using a built-in template for the ListView control, and Table 1 lists the templates that are supported by the ListView control.
Template |
Use |
AlternatingItemTemplate Alternating project Template |
Display alternating items with different tags to make it easier for viewers to distinguish between consecutive items |
EditItemTemplate Edit Project Template |
Control the display of items when editing |
EmptyDataTemplate NULL data template |
Controls the display of ListView data sources when returning empty data |
Emptyitemtemplate Empty Project Template |
Control the display of empty items |
Groupseparatortemplate Group Separator Template |
Control the display of project group content |
GroupTemplate Group Template |
Specify a container object for the content, such as a table row, div, or span component |
InsertItemTemplate Insert Project Template |
Specify content for users when inserting items |
Itemseparatortemplate Project Separator Template |
Control the display of content between projects |
ItemTemplate project Template |
Control the display of project content |
LayoutTemplate Layout Template |
Specifies the root component that defines the container object, such as a table, div, or span component that wraps ItemTemplate or grouptemplate defined content. |
SelectedItemTemplate Selected project template |
Specifies the display of the currently selected item contents |
The two most critical templates are LayoutTemplate and ItemTemplate, as the name implies, LayoutTemplate specifies the total tag for the ListView control.
The tag specified by ItemTemplate is used to display each bound record, for example: The following code shows a string of items that are controlled by an HTML table in ListView.
<asp:ListView ID="..." runat="server" DataSourceID="...">
<LayoutTemplate>
<table …….>
<tr runat="server" ID="itemPlaceholder"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td><%# Eval("Name") %></td>
</tr>
</ItemTemplate>
</asp:ListView>
In the preceding code, the ID of the <tr> tag inside the layouttemplate tag is to set the project placeholder (Itemplaceholder), which tells ListView to pass <table> Where the ItemTemplate generated content is placed, which is why you need to define the LayoutTemplate and ItemTemplate templates separately.
An example of a simple data binding
You've seen multiple templates supported by the Lisview control, and the next step is to create a simple web site called listviewexample (you can get from Http://assets.devx.com/sourcecode/38579_tt_ Mainsource.zip Download the site's sample code), after creating a good Web site, select a Web site? Add a new project, Add a new asp.net page named simplelistview.aspx (see Listing 1), which will use the ListView control to display product data from an product table in the AdventureWorks sample database.