在这段代码中,我试图采用ID为“Label”的控制“标签”,这项工作还需要从实体数据源中获取当前的“AuthorUserID”字段我知道我可以做的这与<%# Eval("AuthorUserID" %>)
,但我想在代码后面的方法这个领域,在这种情况下在“ChatListView_ItemDataBound”方法。Eval在代码隐藏
如何在代码后面加入当前字段(“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>
你为什么想通过这个?如果你只是想格式化或做一些条件 - 然后创建一个函数,如Aspx中的MyFunc,像<%#MyFunc(Eval(“PostDate”))%>,并且在代码中写入函数如字符串MyFunc(object dt){} – Moons 2013-02-16 08:45:19
我想设置e.Item.FindControl(“标签”)为可见= true,如果当前记录的用户ID是与内容的作者ID相同。但你的想法听起来不错,我会尝试。 – TheChampp 2013-02-16 08:47:29
你可以在代码后面使用'Eval(“AuthorUserID”)'',就像你在标记文件中做的那样,如果它在'DataBound'上下文中。 – 2013-02-16 10:42:22