Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 代码隐藏中的Eval_C#_Asp.net_Listview_Eval_Code Behind - Fatal编程技术网

C# 代码隐藏中的Eval

C# 代码隐藏中的Eval,c#,asp.net,listview,eval,code-behind,C#,Asp.net,Listview,Eval,Code Behind,在这段代码中,我试图获取ID为“Label”的控件“Label”,这项工作,但我想从实体数据源获取当前的“AuthorUserID”字段,我知道我可以使用)实现这一点,但我想在代码隐藏方法中获取此字段,在本例中是在“ChatListView\u ItemDataBound”方法中 如何在代码隐藏中获取当前字段(“AuthorUserID”) 代码隐藏: protected void ChatListView_ItemDataBound(object sender, ListViewItemEve

在这段代码中,我试图获取ID为“Label”的控件“Label”,这项工作,但我想从实体数据源获取当前的“AuthorUserID”字段,我知道我可以使用
实现这一点,但我想在代码隐藏方法中获取此字段,在本例中是在“ChatListView\u 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>

根据我们的意见,此代码可能会有所帮助

在页面中创建一些属性,该属性将返回UserID,然后

<ItemTemplate>

  <asp:Label ID="lbl" Visible='<%# UserID == Convert.ToInt32(Eval("AuthorID")) %>' />

</ItemTemplate>

您可以使用
ListViewItemEventArgs e

protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            if (e.Item.FindControl("Label") != null)
            {
               var AuthorUserID = (string)e.Item.DataItem.e.Item.DataItem.AuthorUserID ;
            }
        }
    }
注意 我不知道您是绑定了
类对象
还是绑定了
数据表
,如果绑定了数据表,您应该注意转换存储在
数据项

基本上,
e.Item.DataItem
保存来自数据源的数据

有关更多信息,请参阅:

试试这个

将数据键添加到ListView

<asp:ListView ID="ChatListView" runat="server" OnItemDataBound="ChatListView_ItemDataBound"
        DataKeyNames="AuthorUserID">

试试这个。将标记更改为

<asp:Label ID="Label" runat="server" 
        Text="" 
        Visible='<%# CheckIfAuthorIsUser(Eval("AuthorID")) %>'>
</asp:Label>

你为什么要通过这个考试?如果您只是想格式化它或执行一些条件,那么在aspx中创建一个函数,比如说MyFunc,在代码隐藏中创建一个函数,比如字符串MyFunc(object dt){}。如果当前记录的用户id与内容的作者id相同,我试图将e.Item.FindControl(“Label”)设置为visible=true。但是你的想法听起来不错,我会试试。你可以在代码隐藏中使用
Eval(“AuthorUserID”)
,就像你在标记文件中一样,如果它在
DataBound
上下文中。
<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

}