2

In this code I am trying to take the control "Label" with ID "Label", this work but also I want to take the current "AuthorUserID" field from Entity Data Source I know i can do this with <%# Eval("AuthorUserID" %>) but I want to take this field in code behind method, in this case in "ChatListView_ItemDataBound" method.

How to take the current field ("AuthorUserID") in code behind?

Code-behind:

protected void ChatListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
    if (e.Item.ItemType == ListViewItemType.DataItem)
    {
        if (e.Item.FindControl("Label") != null)
        {
        }
    }
}

Markup:

 <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>
abatishchev
  • 98,240
  • 88
  • 296
  • 433
TheChampp
  • 1,337
  • 5
  • 24
  • 41
  • 2
    Why do you want to pass this? If you just want to format it or do some condition- then create a function say MyFunc in aspx like <%# MyFunc(Eval("PostDate")) %> and in code behind write functions like string MyFunc(object dt) {} – Moons Feb 16 '13 at 08:45
  • I am trying to set e.Item.FindControl("Label") to visible = true if the current logged user id is the same as the author id of the content. But your idea sounds good I will try it. – TheChampp Feb 16 '13 at 08:47
  • You can use `Eval("AuthorUserID")` in Code Behind just like you do in your mark up file if it's in a `DataBound` context. – Henk Mollema Feb 16 '13 at 10:42

4 Answers4

3

Try this. Change Markup as

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

And on code-behind, do this

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

}
naveen
  • 53,448
  • 46
  • 161
  • 251
0

As per our comments may be this code may help

Create some property in page that will return say UserID and then

<ItemTemplate>

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

</ItemTemplate>
Moons
  • 3,833
  • 4
  • 49
  • 82
0

You can reach to the Row data using the 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 ;
            }
        }
    }

Note I don't know if you bounded class object or a datatable, if you bounded a data table you should take care of casting the data which stored in DataItem

Basically the e.Item.DataItem hold the data which comes from your data source

For further information look at:

Community
  • 1
  • 1
Silagy
  • 3,053
  • 2
  • 27
  • 39
0

Try this

Add data key to your ListView

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

And get that key in Code behind

string AuthorUserID = ChatListView.DataKeys[e.Item.DataItemIndex].Value.ToString();
yogi
  • 19,175
  • 13
  • 62
  • 92