ListView.SelectedItemTemplate 属性
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
获取或设置 ListView 控件中的选定项的自定义内容。
public:
virtual property System::Web::UI::ITemplate ^ SelectedItemTemplate { System::Web::UI::ITemplate ^ get(); void set(System::Web::UI::ITemplate ^ value); };
[System.ComponentModel.Browsable(false)]
[System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)]
[System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.ListViewDataItem), System.ComponentModel.BindingDirection.TwoWay)]
public virtual System.Web.UI.ITemplate SelectedItemTemplate { get; set; }
[<System.ComponentModel.Browsable(false)>]
[<System.Web.UI.PersistenceMode(System.Web.UI.PersistenceMode.InnerProperty)>]
[<System.Web.UI.TemplateContainer(typeof(System.Web.UI.WebControls.ListViewDataItem), System.ComponentModel.BindingDirection.TwoWay)>]
member this.SelectedItemTemplate : System.Web.UI.ITemplate with get, set
Public Overridable Property SelectedItemTemplate As ITemplate
属性值
一个对象,包含 ListView 控件中的选定项的自定义内容。 默认值为 null
,指示未设置此属性。
- 属性
示例
以下示例演示如何使用 SelectedItemTemplate 属性在 控件中 ListView 定义选定项的内容。
<%@ Page language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void CategoriesListView_OnItemDeleting(object sender, ListViewDeleteEventArgs e)
{
if (SubCategoriesGridView.Rows.Count > 0)
{
MessageLabel.Text = "You cannot delete a category that has sub-categories.";
e.Cancel = true;
}
}
protected void Page_Load()
{
MessageLabel.Text = String.Empty;
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Subcategories List</title>
</head>
<body>
<form id="form1" runat="server">
<b>Categories</b>
<br />
<asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
<asp:ListView runat="server"
ID="CategoriesListView"
OnItemDeleting="CategoriesListView_OnItemDeleting"
DataSourceID="CategoriesDataSource"
DataKeyNames="ProductCategoryID">
<LayoutTemplate>
<table runat="server" id="tblCategories"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Select" CommandName="Select" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#90EE90">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Delete" CommandName="Delete" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<br />
<b>Subcategories</b>
<asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID"
AutoGenerateColumns="True" />
<!-- 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="CategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductCategoryID], [Name]
FROM Production.ProductCategory">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SubCategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductSubcategoryID], [Name]
FROM Production.ProductSubcategory
WHERE ProductCategoryID = @ProductCategoryID
ORDER BY [Name]">
<SelectParameters>
<asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
ControlID="CategoriesListView" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
<%@ Page language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub CategoriesListView_OnItemDeleting(sender As Object, e As ListViewDeleteEventArgs)
If SubCategoriesGridView.Rows.Count > 0 Then
MessageLabel.Text = "You cannot delete a category that has sub-categories."
e.Cancel = True
End If
End Sub
Protected Sub Page_Load()
MessageLabel.Text = String.Empty
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Subcategories List</title>
</head>
<body>
<form id="form1" runat="server">
<b>Categories</b>
<br />
<asp:Label ForeColor="Red" runat="server" ID="MessageLabel" /><br />
<asp:ListView runat="server"
ID="CategoriesListView"
OnItemDeleting="CategoriesListView_OnItemDeleting"
DataSourceID="CategoriesDataSource"
DataKeyNames="ProductCategoryID">
<LayoutTemplate>
<table runat="server" id="tblCategories"
cellspacing="0" cellpadding="1" width="440px" border="1">
<tr id="itemPlaceholder" runat="server"></tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr runat="server">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Select" CommandName="Select" />
</td>
</tr>
</ItemTemplate>
<SelectedItemTemplate>
<tr runat="server" style="background-color:#90EE90">
<td>
<asp:Label runat="server" ID="NameLabel" Text='<%#Eval("Name") %>' />
</td>
<td style="width:40px">
<asp:LinkButton runat="server" ID="SelectCategoryButton"
Text="Delete" CommandName="Delete" />
</td>
</tr>
</SelectedItemTemplate>
</asp:ListView>
<br />
<b>Subcategories</b>
<asp:GridView runat="server" ID="SubCategoriesGridView" Width="300px"
DataSourceID="SubCategoriesDataSource" DataKeyNames="ProductSubcategoryID"
AutoGenerateColumns="True" />
<!-- 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="CategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductCategoryID], [Name]
FROM Production.ProductCategory">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SubCategoriesDataSource" runat="server"
ConnectionString="<%$ ConnectionStrings:AdventureWorks_DataConnectionString %>"
SelectCommand="SELECT [ProductSubcategoryID], [Name]
FROM Production.ProductSubcategory
WHERE ProductCategoryID = @ProductCategoryID
ORDER BY [Name]">
<SelectParameters>
<asp:ControlParameter Name="ProductCategoryID" DefaultValue="0"
ControlID="CategoriesListView" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</form>
</body>
</html>
注解
SelectedItemTemplate使用 属性为控件中的ListView选定项定义自定义用户界面 (UI) 。 SelectedItemTemplate通常用于区分所选项与其他显示的项。 SelectedItemTemplate 呈现 ,而不是 ItemTemplate 所选项的模板。
若要以声明方式指定自定义模板,请在 控件中添加 ListView 元素SelectedItemTemplate
。 然后,可以将模板的内容添加到 SelectedItemTemplate
元素。 若要显示绑定到控件的数据源的字段值,请使用数据绑定表达式。 有关详细信息,请参阅 数据绑定表达式概述。
还可以将按钮添加到 SelectedItemTemplate。 若要创建执行内置删除或编辑操作的按钮,请将按钮控件添加到模板,并将其 CommandName 属性设置为下表中列出的值之一。
按钮类型 | CommandName 值 |
---|---|
删除 | “Delete” |
编辑 | “编辑” |