3

I'm a bit new to ASP.NET, so bear with me. I came across this solution which would allow me to add an array of strings to a ListView as a row of data. However, I'm not sure on the proper way to set it up. In just trying this solution with a ListView I dropped onto my website, I'm told that new ListViewItem() requires a parameter of type ListViewItemType. The sample solution doesn't provide that, so is it different in ASP.NET?

So the main question I'm asking is: If I have an array of strings, how can I add that to a ListView as a row of data in ASP.NET?

EDIT: I'd like to make sure I'm asking for the right control. I was under the impression a ListView was something like this (but in ASP.NET), but what I'm getting so far is not. It's just a line of text.

2 Answers 2

2

If you want to display like in the example, you want to use GridView instead of ListView.

String collection is not enough to display in multiple columns. You need a collection of objects.

Screen Shot (top is ListView, bottom is GridView)

enter image description here

Sample Code

<asp:ListView runat="server" ID="ListView1">
    <ItemTemplate>
        <%# Eval("Name") %>, 
        <%# Eval("Email") %>, 
        <%# Eval("Phone") %><br />
    </ItemTemplate>
</asp:ListView>
<br/>
<asp:GridView ID="GridView1" AutoGenerateColumns="True" runat="server" />

public class User
{
    public string Name { get; set; }
    public string Email { get; set; }
    public string Phone { get; set; }
}

protected void Page_Load(object sender, EventArgs e)
{
    var collections = new List<User>
        {
            new User {Name = "Jon Doe", Email = "[email protected]", Phone = "123-123-1234"},
            new User {Name = "Marry Doe", Email = "[email protected]", Phone = "456-456-4567"},
            new User {Name = "Eric Newton", Email = "[email protected]", Phone = "789-789-7890"},
        };

    ListView1.DataSource = collections;
    ListView1.DataBind();

    GridView1.DataSource = collections;
    GridView1.DataBind();
}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks! Is it possible to make this selectable?
Yes, you can select/edit/delete a row in GridView. Here are more information about GridView - msdn.microsoft.com/en-us/library/…
It seems a GridView isn't really what I want, since the selection method is different and it seems I have to manually assign the value (an ID field that normally should not be visible). Do you think maybe a DataList is more what I'm looking for? (EDIT: changed DataGrid to DataList)
In my above example, I just want to display the result quick; it's why I use AutoGenerateColumns="True" which displays every column. In real appliction, you want to manually populate the data such as using BoundField. GridView is a big control, so it takes a little bit time to understand it.
Alright, I will look into BoundField. My only issue right now is hiding the ID field (currently it's visible, I'd rather it not be) and making the GridView scrollable, which is probably a whole other thing. Thanks for your help!
|
1

You can convert your array to list with ToList operator

c#

lv.DataSource = yourArray.ToList();
lv.DataBind();

asp.net

<asp:ListView ID="lv" runat="server">

        <ItemTemplate>
            <asp:Label ID="lbl" runat="server" Text='<%# Container.DataItem %>'/>
        </ItemTemplate>

</asp:ListView>

5 Comments

I haven't tried it yet (about to), but this code appears to assign the entire data source of the list view to that one string array. The array of strings is meant to represent one ROW of data, not the entire list that should be in the list view. Am I incorrect in this assumption?
no every string is in row of your list view because i use Container.DataItem,
Alright, so I'm trying to create a ListView with multiple of these...How do I add what you've given me to a list of several? This only lists one result.
i suggest you to add three item template , and bind every ne with property of your list of user List<User>
I'm not sure what you mean. Why three?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.