简体   繁体   中英

What is the best way to pull data into a web form's ListView?

I'm new to web forms, and I would like to build an ASP.NET web application that acts as a client which connects to a WCF service via a TCP connection that pulls information from it and adds this information into a ListView object in my web form. Ideally, I would like to be able to store my data in a list and be able to sort, filter, add, remove, and delete items from this list as messages come in, and update the ListView when these events occur.

What would be the best way to achieve this type of behavior with a ListView, and could you please show me a coded example of how a ListView can bind to a List object which contains rows of data for my ListView, specifically I'd like this List object to contain three strings representing three different cells (one per column) in my ListView so as to create the following ListView in a browser:

    Column 1 Header | Column 2 Header | Column 3 Header
    ---------------------------------------------------
    Cell 1 Text     | Cell 2 Text     | Cell 3 Text
    ---------------------------------------------------
    Cell 4 Text     | Cell 5 Text     | Cell 6 Text
    ---------------------------------------------------
    Cell 7 Text     | Cell 8 Text     | Cell 9 Text

Here's the basic idea.

Have a class that represents the data you want to display in your Listview:

public class SomeStuff
{
    public String string1 { get; set; }
    public String string2 { get; set; }
    public String string3 { get; set; }
}

Then you would generate a list of those objects (from your service, I assume):

List<SomeStuff> allMyDataStuffs = new List<SomeStuff>();
allmyDataStuffs = myWCFService.GetMyData();

And then bind that list to the Listview:

myListView.DataSource = allmyDataStuffs;
myListView.DataBind();

You can then display "string1", "string2", and "string3" however you like in the ItemTemplate of your ListView.

<ItemTemplate>
    <asp:Label ID="string1Lbl" runat="server" Text='<%# Eval("string1")%>' />
    <asp:Label ID="string2Lbl" runat="server" Text='<%# Eval("string2")%>' />
    <asp:Label ID="string3Lbl" runat="server" Text='<%# Eval("string3")%>' />
</ItemTemplate>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM