在 ListView 中动态创建按钮

发布于 2024-11-17 19:41:51 字数 1785 浏览 3 评论 0原文

我有以下问题。我有一个 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

他们创建得很好但它们根本不起作用。我尝试为单击或命令事件添加处理程序,但它也没有帮助。

请帮我解决我的问题!

编辑:

正如 VinayC 建议的那样,我将 ItemDataBound 更改为 ItemCreated。这很有帮助,但我遇到了另一个问题:据我了解,e.Item.DataItem 或者 e.Item 在回发上变成 Nothing,因此按钮不起作用。

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

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

辞别 2024-11-24 19:41:51

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

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

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

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

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.

安静 2024-11-24 19:41:51

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

在 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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文