在ListView中动态创建按钮

时间:2011-06-29 10:37:28

标签: asp.net listview button itemdatabound

我有以下问题。我有一个ListView,它从SQL表返回数据。其中一个栏目看起来像“Ambient / Trance / Goa Trance / House”。

我想做的就是解析这一列并为每个值创建按钮,例如“Ambient”按钮,“Trance”按钮等。

我尝试按以下方式在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所以按钮不起作用。

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

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分: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