2011-06-13 84 views
0

我有一个列表视图中的嵌套列表视图,我试图访问它的项目数据绑定功能,但没有运气可以帮助我解决这个问题吗?我也尝试使用外部列表视图的itemdatabound来完成我试图做的事情,但没有看。 感谢提前:)访问itemdatabound处理器为一个nasched列表视图

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

您可以发布您的代码? – danyolgiax 2011-06-13 13:25:21

回答

1

把它放在ItemCreated事件

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

非常感谢你:)做了这件事:D! – Houlahan 2011-06-13 13:39:33