下面的示例演示如何使用 ListView 控件通过 HTML 表显示数据库中的记录。这些值是使用 LinqDataSource 控件检索的。
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<asp:ListView ID="VendorsListView"
DataSourceID="VendorsDataSource"
DataKeyNames="VendorID"
runat="server">
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" ID="tbl1" runat="server">
<tr runat="server" style="background-color: #98FB98">
<th runat="server">ID</th>
<th runat="server">Account Number</th>
<th runat="server">Name</th>
<th runat="server">Preferred Vendor</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
</td>
<td>
<asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /></td>
<td>
<asp:CheckBox ID="PreferredCheckBox" runat="server"
Checked='<%# Eval("PreferredVendorStatus") %>' Enabled="False" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Add a LINQ -->
<!-- to SQL class to the project to map to a table in -->
<!-- the database. -->
<asp:LinqDataSource ID="VendorsDataSource" runat="server"
ContextTypeName="AdventureWorksClassesDataContext"
Select="new (VendorID, AccountNumber, Name, PreferredVendorStatus)"
TableName="Vendors" Where="ActiveFlag == @ActiveFlag">
<WhereParameters>
<asp:Parameter DefaultValue="true" Name="ActiveFlag" Type="Boolean" />
</WhereParameters>
</asp:LinqDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<asp:ListView ID="VendorsListView"
DataSourceID="VendorsDataSource"
DataKeyNames="VendorID"
runat="server">
<LayoutTemplate>
<table cellpadding="2" width="640px" border="1" ID="tbl1" runat="server">
<tr runat="server" style="background-color: #98FB98">
<th runat="server">ID</th>
<th runat="server">Account Number</th>
<th runat="server">Name</th>
<th runat="server">Preferred Vendor</th>
</tr>
<tr runat="server" id="itemPlaceholder" />
</table>
<asp:DataPager ID="DataPager1" runat="server">
<Fields>
<asp:NumericPagerField />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label ID="VendorIDLabel" runat="server" Text='<%# Eval("VendorID") %>' />
</td>
<td>
<asp:Label ID="AccountNumberLabel" runat="server" Text='<%# Eval("AccountNumber") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' /></td>
<td>
<asp:CheckBox ID="PreferredCheckBox" runat="server"
Checked='<%# Eval("PreferredVendorStatus") %>' Enabled="False" />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Add a LINQ -->
<!-- to SQL class to the project to map to a table in -->
<!-- the database. -->
<asp:LinqDataSource ID="VendorsDataSource" runat="server"
ContextTypeName="AdventureWorksClassesDataContext"
Select="new (VendorID, AccountNumber, Name, PreferredVendorStatus)"
TableName="Vendors" Where="ActiveFlag == @ActiveFlag">
<WhereParameters>
<asp:Parameter DefaultValue="true" Name="ActiveFlag" Type="Boolean" />
</WhereParameters>
</asp:LinqDataSource>
</form>
</body>
</html>
下面的示例演示如何使用 ListView 控件通过 div 元素显示流布局中的值。这些值是使用 SqlDataSource 控件检索的。
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Flow Layout Example</title>
<style type="text/css">
.plainBox {
font-family: Verdana, Arial, sans-serif;
font-size: 11px;
background: #ffffff;
border:1px solid #336666;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Flow Layout Example</h3>
Select the color:
<asp:DropDownList ID="ColorList" runat="server"
AutoPostBack="True"
DataSourceID="ColorDataSource"
DataTextField="Color"
DataValueField="Color">
</asp:DropDownList><br /><br />
<asp:ListView runat="server" ID="ProductListView"
DataSourceID="ProductsDataSource"
DataKeyNames="ProductID">
<LayoutTemplate>
<div runat="server" id="lstProducts">
<div runat="server" id="itemPlaceholder" />
</div>
<asp:DataPager ID="DataPager1" runat="server" PageSize="5" >
<Fields>
<asp:NextPreviousPagerField
ButtonType="Button"
ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:Image ID="ProductImage" runat="server"
ImageUrl='<%# "~/images/thumbnails/" & Eval("ThumbnailPhotoFileName") %>' />
<div class="plainBox" runat="server">
<asp:HyperLink ID="ProductLink" runat="server" Text='<%# Eval("Name") %>'
NavigateUrl='<%# "ProductDetails.aspx?productID=" & Eval("ProductID") %>' />
<br /><br />
<b>Price:</b>
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("ListPrice", "{0:c}")%>' /> <br />
</div>
<br />
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT P.ProductID, P.Name, P.Color, P.ListPrice,
PF.ThumbnailPhotoFileName, P.Size
FROM Production.Product AS P
INNER JOIN Production.ProductProductPhoto AS PPF ON P.ProductID = PPF.ProductID
INNER JOIN Production.ProductPhoto AS PF ON PPF.ProductPhotoID = PF.ProductPhotoID
WHERE P.Color = @Color" >
<SelectParameters>
<asp:ControlParameter ControlID="ColorList" Name="Color"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ColorDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DISTINCT Color FROM Production.Product">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Flow Layout Example</title>
<style type="text/css">
.plainBox {
font-family: Verdana, Arial, sans-serif;
font-size: 11px;
background: #ffffff;
border:1px solid #336666;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Flow Layout Example</h3>
Select the color:
<asp:DropDownList ID="ColorList" runat="server"
AutoPostBack="True"
DataSourceID="ColorDataSource"
DataTextField="Color"
DataValueField="Color">
</asp:DropDownList><br /><br />
<asp:ListView runat="server" ID="ProductListView"
DataSourceID="ProductsDataSource"
DataKeyNames="ProductID">
<LayoutTemplate>
<div runat="server" id="lstProducts">
<div runat="server" id="itemPlaceholder" />
</div>
<asp:DataPager runat="server" PageSize="5" >
<Fields>
<asp:NextPreviousPagerField
ButtonType="Button"
ShowFirstPageButton="True"
ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</LayoutTemplate>
<ItemTemplate>
<asp:Image ID="ProductImage" runat="server"
ImageUrl='<%# "~/images/thumbnails/" + Eval("ThumbnailPhotoFileName") %>' />
<div class="plainBox" runat="server">
<asp:HyperLink ID="ProductLink" runat="server" Text='<%# Eval("Name") %>'
NavigateUrl='<%# "ProductDetails.aspx?productID=" + Eval("ProductID") %>' />
<br /><br />
<b>Price:</b>
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("ListPrice", "{0:c}")%>' /> <br />
</div>
<br />
</ItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="ProductsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT P.ProductID, P.Name, P.Color, P.ListPrice,
PF.ThumbnailPhotoFileName, P.Size
FROM Production.Product AS P
INNER JOIN Production.ProductProductPhoto AS PPF ON P.ProductID = PPF.ProductID
INNER JOIN Production.ProductPhoto AS PF ON PPF.ProductPhotoID = PF.ProductPhotoID
WHERE P.Color = @Color" >
<SelectParameters>
<asp:ControlParameter ControlID="ColorList" Name="Color"
PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="ColorDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DISTINCT Color FROM Production.Product">
</asp:SqlDataSource>
</form>
</body>
</html>
下面的示例演示如何使用 ListView 控件插入、删除和更新记录。
安全说明: |
|---|
此示例有一个接受用户输入的文本框,这是一个潜在的安全威胁。默认情况下,ASP.NET 网页验证用户输入是否不包括脚本或 HTML 元素。有关更多信息,请参见脚本侵入概述。 |
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
<style type="text/css">
.EditItem { background-color:#8FBC8F;}
.SelectedItem { background-color:#9ACD32; }
.InsertItem { background-color:#FFFACD;}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<h5>Departments</h5>
<asp:ListView ID="DepartmentsListView"
DataSourceID="DepartmentsDataSource"
DataKeyNames="DepartmentID"
ConvertEmptyStringToNull="true"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblDepartments" width="640px" cellspacing="0">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Button ID="SelectButton" runat="server" Text="Select" CommandName="Select" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr class="SelectedItem" runat="server">
<td>
<asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</SelectedItemTemplate>
<EditItemTemplate>
<tr class="EditItem">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<b>ID</b><br />
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server"
Width="200px"
Text='<%#Bind("GroupName") %>' />
<br />
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr class="InsertItem">
<td colspan="2">
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server" Text='<%#Bind("GroupName") %>' />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="DepartmentsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
UpdateCommand="UPDATE HumanResources.Department
SET Name = @Name, GroupName = @GroupName WHERE (DepartmentID = @DepartmentID)"
DeleteCommand="DELETE FROM HumanResources.Department
WHERE (DepartmentID = @DepartmentID)"
InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName)
VALUES (@Name, @GroupName)">
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html >
<head id="Head1" runat="server">
<title>ListView Example</title>
<style type="text/css">
.EditItem { background-color:#8FBC8F;}
.SelectedItem { background-color:#9ACD32; }
.InsertItem { background-color:#FFFACD;}
</style>
</head>
<body>
<form id="form1" runat="server">
<h3>ListView Example</h3>
<h5>Departments</h5>
<asp:ListView ID="DepartmentsListView"
DataSourceID="DepartmentsDataSource"
DataKeyNames="DepartmentID"
ConvertEmptyStringToNull="true"
InsertItemPosition="LastItem"
runat="server">
<LayoutTemplate>
<table cellpadding="2" runat="server" id="tblDepartments" width="640px" cellspacing="0">
<tr runat="server" id="itemPlaceholder" />
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Button ID="SelectButton" runat="server" Text="Select" CommandName="Select" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr class="SelectedItem" runat="server">
<td>
<asp:Button ID="DeleteButton" runat="server" Text="Delete" CommandName="Delete" />
<asp:Button ID="EditButton" runat="server" Text="Edit" CommandName="Edit" />
</td>
<td>
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label ID="NameLabel" runat="server" Text='<%#Eval("Name") %>' />
</td>
<td>
<asp:Label ID="GroupNameLabel" runat="server" Text='<%#Eval("GroupName") %>' />
</td>
</tr>
</SelectedItemTemplate>
<EditItemTemplate>
<tr class="EditItem">
<td>
<asp:Button ID="UpdateButton" runat="server" CommandName="Update" Text="Update" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<b>ID</b><br />
<asp:Label ID="IDLabel" runat="server" Text='<%#Eval("DepartmentID") %>' />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server"
Width="200px"
Text='<%#Bind("GroupName") %>' />
<br />
</td>
</tr>
</EditItemTemplate>
<InsertItemTemplate>
<tr class="InsertItem">
<td colspan="2">
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Cancel" />
</td>
<td>
<asp:Label runat="server" ID="NameLabel" AssociatedControlID="NameTextBox"
Text="Name" Font-Bold="true"/><br />
<asp:TextBox ID="NameTextBox" runat="server" Text='<%#Bind("Name") %>' /><br />
</td>
<td>
<asp:Label runat="server" ID="GroupNameLabel" AssociatedControlID="GroupNameTextBox"
Text="Group Name" Font-Bold="true" /><br />
<asp:TextBox ID="GroupNameTextBox" runat="server" Text='<%#Bind("GroupName") %>' />
</td>
</tr>
</InsertItemTemplate>
</asp:ListView>
<!-- This example uses Microsoft SQL Server and connects -->
<!-- to the AdventureWorks sample database. Use an ASP.NET -->
<!-- expression to retrieve the connection string value -->
<!-- from the Web.config file. -->
<asp:SqlDataSource ID="DepartmentsDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT DepartmentID, Name, GroupName FROM HumanResources.Department"
UpdateCommand="UPDATE HumanResources.Department
SET Name = @Name, GroupName = @GroupName WHERE (DepartmentID = @DepartmentID)"
DeleteCommand="DELETE FROM HumanResources.Department
WHERE (DepartmentID = @DepartmentID)"
InsertCommand="INSERT INTO HumanResources.Department(Name, GroupName)
VALUES (@Name, @GroupName)">
</asp:SqlDataSource>
</form>
</body>
</html>