2011-06-13 86 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