我使用ListView
来显示存储在表中的用户信息。我只想展示一张唱片。
当用户第一次开始使用网站时,不会有初始记录,所以我需要InsertItemTemplate
是可见的。插入记录后,我需要InsertItemTemplate
消失。
基于研究,我使用DataBound
事件来检查ListView.Item.Count
,并相应地将InsertItemPosition
设置为None
或LastItem
。我还在页面上临时设置了一个标签,以显示计数结果。
标签正在正确更新。然而,InsertItemPosition
直到出现第二个postback
之后才改变。因此,结果与它应该是的相反。当页面加载时,它可以正常工作,但在我插入数据后保持可见,然后如果我删除记录,它就会消失。如有任何帮助,我们将不胜感激。我已经在下面发布了相关代码。
protected void ListView1_DataBound(object sender, EventArgs e)
{
if (ListView1.Items.Count > 0)
{
lblRecordCount.Text = "DataBound - Records exist";
ListView1.InsertItemPosition = InsertItemPosition.None;
}
else
{
lblRecordCount.Text = "DataBound - No records exist";
ListView1.InsertItemPosition = InsertItemPosition.LastItem;
}
}
Record Count: <asp:Label ID="lblRecordCount" runat="server" Text="Nothing Happened"></asp:Label>
<br />
<asp:ListView ID="ListView1" runat="server" DataKeyNames="id" DataSourceID="SqlDataSourceGift" OnItemCreated ="ListView1_ItemCreated" OnDataBound="ListView1_DataBound" >
在update panel
中添加ListView
,然后尝试显示/隐藏InsertItemTemplate
。