0

I have a following problem. I have a ListView which returns data from SQL table. One of its columns looks like "Ambient/Trance/Goa Trance/House".

All i want to do is parse this column and create buttons for each value, for example a button for "Ambient", a button for "Trance", etc.

I tried to create buttons in ItemDataBound event in the following way:

    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

They created fine but they didn't work at all. I tried to Add Handler for Click or Command event but it also didn't help.

Please help me to solve my problem!

Edit:

As VinayC suggested I changed ItemDataBound to ItemCreated. That helped, but I faced another problem: as far as I understand e.Item.DataItem or, maybe, e.Item becomes Nothing on PostBacks so the buttons do not work.

How to solve that problem? Thanks once again!

2 Answers2

1

I believe that buttons must be getting created late in page life cycle and hence not responding to events.

You may want to try moving your code in ItemCreated event and use ListView's ItemCommand event to trap these. Yet another suggestion is to assign (different) ID to your link buttons - for example

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

In case, you want to attach an click event handler directly to buttons then ID is must.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • Thanks! That helped me! But I have one more questions. I have multiple ListViews on the page and I bind them to SQLDataSources dynamically. ItemCreated event throws an error when I do ListView.DataSource = Nothing ListView.DataBind(). How can I avoid it. And, by the way, when I press on dynamically generated buttons they fire but dissapear then. What can I do with it? – Vjacheslav Ravdin Jun 29 '11 at 13:01
  • Sorry, I was mistaken. It didn't help. The buttons do not send CommandNames and CommandArguments. – Vjacheslav Ravdin Jun 29 '11 at 13:08
0

So, I solved my problem. The solution wasn't simple but here it is:

In ItemCreated event I firstly count the number of buttons, then save it to ViewState, and only then I create buttons. I had to save the number of buttons to ViewState because on every postback e.Item.DataItem becomes Nothing.

Maybe there is a simplier solution but I found only that one...

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