【问题标题】:Eval In code-behind评估代码隐藏
【发布时间】: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


【解决方案1】:

试试这个。将标记更改为

<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

}

【讨论】:

    【解决方案2】:

    您可以使用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 ;
                }
            }
        }
    

    注意 我不知道你是绑定class object还是datatable,如果你绑定了一个数据表,你应该注意转换存储在DataItem中的数据

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

    更多信息请看:

    【讨论】:

      【解决方案3】:

      试试这个

      将数据键添加到您的 ListView

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

      并在 Code behind 中获取该密钥

      string AuthorUserID = ChatListView.DataKeys[e.Item.DataItemIndex].Value.ToString();
      

      【讨论】:

        【解决方案4】:

        根据我们的 cmets,这段代码可能会有所帮助

        在页面中创建一些属性,然后返回用户 ID

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-11-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-10-25
          • 2014-07-26
          • 2018-12-11
          • 2011-01-25
          相关资源
          最近更新 更多