在这段代码中,我尝试采用ID为“ Label”的控件“ Label”,但我也想从实体数据源中获取当前的“ AuthorUserID”字段,我知道我可以使用<%# Eval("AuthorUserID" %>)
来做到这一点,但我想在方法后面的代码中采用此字段,在本例中为“ ChatListView_ItemDataBound”方法。
如何在后面的代码中获取当前字段(“ AuthorUserID”)?
后台代码:
protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
if (e.Item.ItemType == ListViewItemType.DataItem)
{
if (e.Item.FindControl("Label") != null)
{
}
}
}
标记:
<asp:ListView ID="ChatListView" runat="server" DataSourceID="EntityDataSourceUserPosts" OnItemDataBound="ChatListView_ItemDataBound">
<ItemTemplate>
<div class="post">
<div class="postHeader">
<h2><asp:Label ID="Label1" runat="server"
Text= '<%# Eval("Title") + " by " + this.GetUserFromPost((Guid?)Eval("AuthorUserID")) %>' ></asp:Label></h2>
<asp:Label ID="Label" runat="server" Text="" Visible="True"></asp:Label>
<div class="dateTimePost">
<%# Eval("PostDate")%>
</div>
</div>
<div class="postContent">
<%# Eval("PostComment") %>
</div>
</div>
</ItemTemplate>
</asp:ListView>
最佳答案
尝试这个。将标记更改为
<asp:Label ID="Label" runat="server"
Text=""
Visible='<%# CheckIfAuthorIsUser(Eval("AuthorID")) %>'>
</asp:Label>
然后在代码背后,执行此操作
protected bool CheckIfAuthorIsUser(object authorID)
{
if(authorID == null){ return false;}
//else compare the passed authorid parameter with the logged in userid and return the appropriate boolean value
}