C# 通过将控件的 ID 属性设置为“itemPlaceholder”来指定项目占位符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1897581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Specify an item placeholder by setting a control's ID property to "itemPlaceholder"
提问by Asad
I have only single "Default.aspx" page and a single ListView Control. Why am I getting this error. Never Happened before
我只有一个“Default.aspx”页面和一个 ListView 控件。为什么我收到这个错误。从未发生过
"An item placeholder must be specified on ListView 'ListView1'. Specify an item placeholder by setting a control's ID property to "itemPlaceholder". The item placeholder control must also specify runat="server"."
“必须在 ListView 'ListView1' 上指定项占位符。通过将控件的 ID 属性设置为“itemPlaceholder”来指定项占位符。项占位符控件还必须指定 runat="server"。
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TesterConcepts._Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
onselecting="ObjectDataSource1_Selecting" SelectMethod="GetItemsCollection"
TypeName="TesterConcepts.CutomDataSource">
<SelectParameters>
<asp:Parameter Name="items" Type="Object" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1"
onselectedindexchanged="ListView1_SelectedIndexChanged">
</asp:ListView>
</body>
</html>
doing this was not helpful even
这样做甚至没有帮助
<asp:ListView ID="ListView1" runat="server" DataSourceID="ObjectDataSource1"
OnSelectedIndexChanged="ListView1_SelectedIndexChanged"
ItemPlaceholderID="PlaceHolder1">
</asp:ListView>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
Now it throws this exception
现在它抛出这个异常
"An item placeholder must be specified on ListView 'ListView1'. Specify an item placeholder by setting a control's ID property to "PlaceHolder1". The item placeholder control must also specify runat="server""
“必须在 ListView 'ListView1' 上指定项占位符。通过将控件的 ID 属性设置为“PlaceHolder1”来指定项占位符。项占位符控件还必须指定 runat="server""
采纳答案by Asad
Looks like you need to define the placeholder element structure for the item elements which the query will return.
看起来您需要为查询将返回的项目元素定义占位符元素结构。
I'd suggest reading this article. A little old, but illustrates the concept. http://www.4guysfromrolla.com/articles/122607-1.aspx
我建议阅读这篇文章。有点旧,但说明了这个概念。 http://www.4guysfromrolla.com/articles/122607-1.aspx
回答by Ananth
In ListView, Layout Template is the template which decides the Layout of the data display . It should have an item placeholder tag with runat=”server” attribute.
在 ListView 中,Layout Template 是决定数据显示布局的模板。它应该有一个带有 runat=”server” 属性的项目占位符标签。
Since the ListView's LayoutTemplate and ItemTemplate are each defined separately, we need some way to tell the LayoutTemplate, "Hey, for each record you are displaying, put the rendered item markup here." This is accomplished by adding a server-side control with the ID value specified by the ListView's ItemPlaceholderID property.
由于 ListView 的 LayoutTemplate 和 ItemTemplate 分别定义,我们需要某种方式告诉 LayoutTemplate,“嘿,对于您正在显示的每条记录,将呈现的项目标记放在这里。” 这是通过添加具有由 ListView 的 ItemPlaceholderID 属性指定的 ID 值的服务器端控件来实现的。
Ref - http://www.4guysfromrolla.com/articles/122607-1.aspx
参考 - http://www.4guysfromrolla.com/articles/122607-1.aspx
Hence U'll have to 1)Define a ItemsTemplate 2)Add a Placeholder in the LayoutTemplate
因此你必须 1) 定义一个 ItemsTemplate 2) 在 LayoutTemplate 中添加一个占位符
<tr runat="server" id="itemPlaceholder">
</tr>
or
或者
<ItemTemplate>
<tr>
<td>
<asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Item") %>' />
</td>
<td>
<asp:Label ID="URLLabel" runat="server" Text='<%# Eval("URL") %>' />
</td>
</tr>
</ItemTemplate>
So the final Design will look like
所以最终的设计看起来像
<asp:ListView ID="NoticeItemsListView" runat="server">
<LayoutTemplate>
<table width="200px">
<tr>
<th>
Message
</th>
<th>
URL
</th>
</tr>
<tr runat="server" id="itemPlaceholder">
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="MessageLabel" runat="server" Text='<%# Eval("Item") %>' />
</td>
<td>
<asp:Label ID="URLLabel" runat="server" Text='<%# Eval("URL") %>' />
</td>
</tr>
</ItemTemplate>
</asp:ListView>
回答by Mohamed.Abdo
Inside your ListView you've to add a LayoutTemplate containing the PlaceHolder:
在您的 ListView 中,您必须添加一个包含 PlaceHolder 的 LayoutTemplate:
<asp:ListView ID="listview1" runat="server" ItemPlaceholderID="PlaceHolder1" >
<LayoutTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</LayoutTemplate>
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>