user1801745
user1801745

Reputation: 389

How to populate an asp listbox with multiple values with LINQ?

My listbox doesnt appear data, only the classdata name:

    XDocument doc = XDocument.Parse(XMLfile);
    List<ClassData> data = (from item in doc.Descendants("freq")
         where item.Element("ID").Value > 50
         orderby item.Element("Time").Value
         select new ClassData
         {
              ID= item.Element("ID").Value,
              Name= item.Element("Name").Value,
              Age= item.Element("Age").Value,
              Time= item.Element("Time").Value
          }).ToList<ClassData>();
    lstBox.DataSource = data;
    lstBox.DataBind();

myclassdata:

 public class ClassData
{
    public string ID{ get; set; }
    public string Name{ get; set; }
    public string Age{ get; set; }
    public string Time{ get; set; }
}

I add 4 items in my listbox with same name of my classData Items... my result is: SolutionName.ClassData 32 times (number of results)

Upvotes: 0

Views: 700

Answers (2)

user1801745
user1801745

Reputation: 389

 <asp:DataList ID="dataList" runat="server"
        RepeatColumns="1"
        RepeatDirection="Horizontal"
        RepeatLayout="Table">
        <ItemTemplate>
            <table border="1" >
                <tr>
                    <td><asp:Label ID="Label1" runat="server" Text='<%# Eval("ID") %>' Width="50" /></td>
                    <td><asp:Label ID="Label2" runat="server" Text='<%# Eval("Name") %>' Width="20" /></td>
                    <td><asp:Label ID="Label3" runat="server" Text='<%# Eval("Age") %>' Width="200" /></td>
                    <td><asp:Label ID="Label4" runat="server" Text='<%# Eval("Time") %>' Width="200" /></td>
                </tr>
            </table>
        </ItemTemplate>
    </asp:DataList>

Upvotes: 0

J. Ed
J. Ed

Reputation: 6742

defaultly, the listbox displays the result of ToString() of the items you provided, which is 'SolutionName.ClassData' like you said.
To change that, you need to change the DisplayMember property of the listbox- listBox.DisplayMember = "Name"
for asp.net listbox, it's DataTextField

Upvotes: 1

Related Questions