访问嵌套列表视图的itemdatabound处理程序

时间:2011-06-13 13:21:35

标签: asp.net vb.net listview

我在列表视图中有一个嵌套列表视图,我正在尝试访问其项目数据绑定功能,但没有运气,任何人都可以帮我解决这个问题?香港专业教育学院也尝试使用外部listview的itemdatabound来做我试图做的事情,但没有看。 在此先感谢:)

    Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
    'determins if you created the comment
    If ListView1.EditIndex >= 0 Then

        Dim dataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
        If dataItem.DisplayIndex = ListView1.EditIndex Then

            Dim postid As String = DirectCast(DataBinder.Eval(dataItem.DataItem, "Post"), String)
            Dim commentTxt As TextBox = DirectCast(e.Item.FindControl("EditPostTxt"), TextBox)
            commentTxt.Text = postid
        End If
    End If

    Try
        Dim GetdataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)
        Dim getPostId As Guid = DirectCast(DataBinder.Eval(GetdataItem.DataItem, "PostId"), Guid)

        Dim listview As ListView = DirectCast(e.Item.FindControl("CommentsOnPosts"), ListView)
        Dim loadcomments = From p In db.CommentPosts Where p.PostId = getPostId Select p.CommentPostId, p.PostId, p.aspnet_User.Tmp_Profile.DisaplyPictureSmall, fullname = p.aspnet_User.FirstName & " " & p.aspnet_User.LastName, profileid = p.aspnet_User.Tmp_Profile.ProfileId.ToString, p.Post, p.Date Order By [Date] Descending

        listview.DataSource = loadcomments
        listview.DataBind()
        Try
            cc.aloudToSeeDeleteSubPost(e, User.Identity.Name, "CommentDeleteTxt", "CommentEditTxt")
        Catch ex As Exception
            a.Show(ex.ToString)
        End Try
    Catch ex As Exception
        a.Show(ex.ToString)
    End Try

    cc.aloudToSeeDelete(e, User.Identity.Name, "linkbutton1", "linkbutton2")

End Sub

    Public Sub aloudToSeeDeleteSubPost(ByVal e As ListViewItemEventArgs, ByVal username As String, ByVal DelButton As String, ByVal editBut As String)
    If e.Item.ItemType = ListViewItemType.DataItem Then
        Dim dataItem As ListViewDataItem = DirectCast(e.Item, ListViewDataItem)

        If dataItem.DataItem IsNot Nothing Then
            Dim postid As Guid = DirectCast(DataBinder.Eval(dataItem.DataItem, "PostId"), Guid)

            Dim getMessageFrom = (From p In db.CommentPosts Where p.PostId = postid).Single
            If Not getMessageFrom.FromId = uif.returnUserID(username) Then
                Dim DeleteButton As LinkButton = DirectCast(e.Item.FindControl(DelButton), LinkButton)
                Dim EditButton As LinkButton = DirectCast(e.Item.FindControl(editBut), LinkButton)
                DeleteButton.Visible = False
                EditButton.Visible = False
            End If
            If getMessageFrom.ToId = uif.returnUserID(username) Then
                Dim DeleteButton As LinkButton = DirectCast(e.Item.FindControl(DelButton), LinkButton)
                DeleteButton.Visible = True
            End If
        End If
    End If
End Sub

1 个答案:

答案 0 :(得分:1)

将其放入ItemCreated事件

Dim listview As ListView = DirectCast(e.Item.FindControl("CommentsOnPosts"), ListView)
AddHandler listview.ItemDataBound, AddressOf ListViewCommentsOnPosts_ItemDataBound