I have a ListView which is filled by generic list of type MyClass. I can easily bind data from this list into ListView. But I have problems with reading data in opposite direction. This is my class:

public class MyClass 
{ 
    public int id { get; set; } 
    public string name { get; set; } 
}

I have also generic list of type MyClass:

List<MyTest> list = new List<MyTest>();

Finally I bind data to ListView this way:

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

My ListView template:

<asp:ListView runat="server" ID="ListView1"> 
  <LayoutTemplate> 
    <table runat="server" id="table1" > 
      <tr runat="server" id="itemPlaceholder" ></tr> 
    </table> 
  </LayoutTemplate> 
  <ItemTemplate> 
    <tr id="tr" runat="server"> 
        <td id="td1" runat="server"> 
            <asp:TextBox ID="tb1" runat="server" Text='<%#Eval("id") %>' /> 
        </td> 
        <td id="td2" runat="server"> 
            <asp:TextBox ID="tb2" runat="server" Text='<%#Eval("name") %>' /> 
        </td> 
    </tr> 
  </ItemTemplate> 
</asp:ListView>

How can I read data from ListView into my List list?

The operation of reading ListView data into List generic list should begin after clicking the button "GetData".

I saw the following advice:

List<MyClass> list = ListView1.Items 
                              .Select(lvi=>lvi.DataItem as MyClass) 
                              .ToList();

but it does not work - the DataItem property of each Item of ListView has null value.

The point is that I have generic list with data. I am binding this data into ListView which can be edited by user. After that after click Save or GetData data from ListView is read into generic list of MyClass type.

Eh, I expect the LINQ to work, but yo uwere right it returns null, however. Why can't you simply use the datasource property?

List<Foo> foos = new List<Foo>();
                        foos.Add( new Foo() { Item1 = "dani" , Item2 = "web" } );
                        foos.Add( new Foo() { Item1 = "stack" , Item2 = "overflow" } );

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

                        foos.Add( new Foo() { Item1 = "test2" , Item2 = "success" } );

                        List<Foo> foos2 = ListView1.DataSource as List<Foo>;

Foo =

public class Foo
        {
                public string Item1 { get; set; }
                public string Item2 { get; set; }
        }

Even if you add another Item to the list afterward, since datasource is a reference to the list. Your going to have the correct list result.

Eh, I expect the LINQ to work, but yo uwere right it returns null, however. Why can't you simply use the datasource property?

List<Foo> foos = new List<Foo>();
                        foos.Add( new Foo() { Item1 = "dani" , Item2 = "web" } );
                        foos.Add( new Foo() { Item1 = "stack" , Item2 = "overflow" } );

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

                        foos.Add( new Foo() { Item1 = "test2" , Item2 = "success" } );

                        List<Foo> foos2 = ListView1.DataSource as List<Foo>;

Foo =

public class Foo
        {
                public string Item1 { get; set; }
                public string Item2 { get; set; }
        }

Even if you add another Item to the list afterward, since datasource is a reference to the list. Your going to have the correct list result.

ok. Your solution is working only when we put all the code into one method.
But when we will do something like this:

protected void btnBind_Click(object sender, EventArgs e)
    {
      List<Foo> foos = new List<Foo>();
      foos.Add( new Foo() { Item1 = "dani" , Item2 = "web" } );
      foos.Add( new Foo() { Item1 = "stack" , Item2 = "overflow" } );
      ListView1.DataSource = foos;
      ListView1.DataBind();
    }
protected void btnGet_Click(object sender, EventArgs e)
    {
        List<Foo> foos2 = ListView1.DataSource as List<Foo>;
    }

in second event DataSource property of ListView1 object has null value.

Why can't you store foos in ViewState in btnBind_Click event ? then in the btnGet_Click just Cast the ViewState to List<Foo> type.

I believe it will work for you..

ok. Your solution is working only when we put all the code into one method.
But when we will do something like this:

protected void btnBind_Click(object sender, EventArgs e)
    {
      List<Foo> foos = new List<Foo>();
      foos.Add( new Foo() { Item1 = "dani" , Item2 = "web" } );
      foos.Add( new Foo() { Item1 = "stack" , Item2 = "overflow" } );
      ListView1.DataSource = foos;
      ListView1.DataBind();
    }
protected void btnGet_Click(object sender, EventArgs e)
    {
        List<Foo> foos2 = ListView1.DataSource as List<Foo>;
    }

in second event DataSource property of ListView1 object has null value.

List<MyClass> list = (List<MyClass>)ListView1.DataSource

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.