Search code examples
vb.netlistviewvisual-web-developer-2010

How do I create and populate a 3 column ListView?


I need to create and populate a ListView with 3 strings that come from another function. I also want to be able to select multiple pieces of the data to change their values during runtime (is that possible with ListView?).

I've looked all over online for info on this, but I can't seem to find any. If someone would could give me some insight on the best way to do this, I would really appreciate it.

I've seen somethings on GridView as well. Would that be better for this application?


Solution

  • I must admit that i've no clue what you're actually asking. But yes, you can bind 3 Strings to a ListView coming from "another function" :)

    You can handle ItemDataBound to change values in the ListItems:

    <asp:ListView runat="server" ID="ListView1" >
        <LayoutTemplate>
            <table>
                <thead>
                    <tr>
                        <th>
                            A String
                        </th>
                    </tr>
                </thead>
                <tbody>
                    <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                </tbody>
            </table>
        </LayoutTemplate>
        <ItemTemplate>
            <tr>
                <td>
                   <asp:Label ID="LblString" Text="<%# Container.DataItem %>" runat="server"></asp:Label>
                </td>
            </tr>
        </ItemTemplate>
    </asp:ListView>
    

    The page's codebehind:

    Public Class ListView
        Inherits System.Web.UI.Page
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            If Not IsPostBack Then
                Me.ListView1.DataSource = OtherFuntion()
                Me.ListView1.DataBind()
            End If
        End Sub
    
        Private Function OtherFuntion() As IEnumerable(Of String)
            Return {"String 1", "String 2", "String 3"}
        End Function
    
        Private Sub ListView1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView1.ItemDataBound
            If e.Item.ItemType = ListViewItemType.DataItem Then
                Dim lblString = DirectCast(e.Item.FindControl("LblString"), Label)
                lblString.Text = New String(lblString.Text.Reverse.ToArray)
            End If
        End Sub
    
    End Class
    

    This simply reverses all strings.