【发布时间】:2013-02-01 05:50:04
【问题描述】:
在这段代码中,我尝试使用 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>
【问题讨论】:
-
为什么要通过这个?如果你只是想格式化它或做一些条件 - 然后在 aspx 中创建一个函数说 MyFunc,比如 并在后面的代码中编写函数,比如 string MyFunc(object dt) {}
-
如果当前记录的用户 ID 与内容的作者 ID 相同,我正在尝试将 e.Item.FindControl("Label") 设置为 visible = true。但是你的想法听起来不错,我会试试的。
-
如果
DataBound上下文中,您可以像在标记文件中一样在代码隐藏中使用Eval("AuthorUserID")。
标签: c# asp.net listview eval code-behind