0

我有以下问题。我有一个从 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,因此按钮不起作用。

如何解决这个问题?再次感谢!

4

2 回答 2

1

我相信按钮必须在页面生命周期的后期创建,因此不响应事件。

您可能想尝试在ItemCreated事件中移动您的代码并使用 ListView 的ItemCommand事件来捕获这些。另一个建议是为您的链接按钮分配(不同的)ID - 例如

ListView_Albums_LinkButton_Artist.ID = "A" & Artists(Integer1)

如果您想将单击事件处理程序直接附加到按钮,则必须使用 ID。

于 2011-06-29T11:31:26.583 回答
0

所以,我解决了我的问题。解决方案并不简单,但它是:

在 ItemCreated 事件中,我首先计算按钮的数量,然后将其保存到 ViewState,然后才创建按钮。我不得不将按钮的数量保存到 ViewState,因为在每次回发时 e.Item.DataItem 都变成了 Nothing。

也许有一个更简单的解决方案,但我发现只有一个......

Sub OnItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs)
    Dim ListView_Albums_PlaceHolder_Artists As PlaceHolder = e.Item.FindControl("ListView_Albums_PlaceHolder_Artists")

    If Not ListView_Albums_PlaceHolder_Artists Is Nothing Then
        If Not e.Item.DataItem Is Nothing Then
            ViewState("Length") = e.Item.DataItem("album_artists").ToString.Split("/").Length
        End If

        If Not ViewState("Length") Is Nothing Then
            Dim Length As Integer = ViewState("Length")

            For Integer1 As Integer = 0 To Length - 1
                Dim ListView_Albums_LinkButton_Artist As New LinkButton
                ListView_Albums_LinkButton_Artist.ID = "ListView_Albums_LinkButton_Artist_" & Integer1

                ListView_Albums_PlaceHolder_Artists.Controls.Add(ListView_Albums_LinkButton_Artist)
            Next
        End If
    End If
End Sub

Sub OnItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs)
    Dim ListView_Albums_PlaceHolder_Artists As PlaceHolder = e.Item.FindControl("ListView_Albums_PlaceHolder_Artists")

    If Not ListView_Albums_PlaceHolder_Artists Is Nothing Then
        If Not e.Item.DataItem Is Nothing Then
            Dim Artists As String() = e.Item.DataItem("album_artists").ToString.Split("/")
            Dim Artists_N As String() = e.Item.DataItem("album_artists_n").ToString.Split("/")

            For Integer1 As Integer = 0 To Artists.Length - 1
                Dim ListView_Albums_LinkButton_Artist As LinkButton = e.Item.FindControl("ListView_Albums_LinkButton_Artist_" & Integer1)

                ListView_Albums_LinkButton_Artist.CommandArgument = Artists(Integer1)
                ListView_Albums_LinkButton_Artist.Text = Artists_N(Integer1)
                ListView_Albums_LinkButton_Artist.CssClass = "a-03"
            Next
        End If
    End If
End Sub
于 2011-06-30T13:44:09.503 回答