我有以下问题。我有一个从 SQL 表返回数据的 ListView。其中一列看起来像“环境/恍惚/果阿恍惚/房子”。
我要做的就是解析此列并为每个值创建按钮,例如“环境”按钮、“恍惚”按钮等。
我尝试通过以下方式在 ItemDataBound 事件中创建按钮:
Dim ListView_Albums_PlaceHolder_Artists As PlaceHolder = e.Item.FindControl("ListView_Albums_PlaceHolder_Artists")
Dim Artists As String() = e.Item.DataItem("album_artists").ToString.Split("/")
Dim ArtistsN As String() = e.Item.DataItem("album_artists_n").ToString.Split("/")
Dim ListView_Albums_Literal_Artists As New Literal
If Artists.Length = 1 Then
ListView_Albums_Literal_Artists.Text = "Artist: "
Else
ListView_Albums_Literal_Artists.Text = "Artists: "
End If
ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_Literal_Artists)
For Integer1 As Integer = 0 To Artists.Length - 1
Dim ListView_Albums_LinkButton_Artist As New LinkButton
ListView_Albums_LinkButton_Artist.Text = ArtistsN(Integer1)
ListView_Albums_LinkButton_Artist.CommandName = "Artist"
ListView_Albums_LinkButton_Artist.CommandArgument = Artists(Integer1)
ListView_Albums_LinkButton_Artist.CssClass = "a-03"
ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_LinkButton_Artist)
Dim ListView_Albums_Literal As New Literal
ListView_Albums_Literal.Text = ", "
If Not Integer1 = Artists.Length - 1 Then
ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_Literal)
End If
Next
他们创造得很好,但他们根本没有工作。我尝试为 Click 或 Command 事件添加处理程序,但它也没有帮助。
请帮我解决我的问题!
编辑:
正如 VinayC 建议的那样,我将 ItemDataBound 更改为 ItemCreated。这有帮助,但我遇到了另一个问题:据我了解 e.Item.DataItem 或者,也许 e.Item 在 PostBacks 上变为 Nothing,因此按钮不起作用。
如何解决这个问题?再次感谢!