简体   繁体   English

如何使用LINQ填充具有多个值的ASP列表框?

[英]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: 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) 我在列表框中添加了与我的classData项目名称相同的4个项目...我的结果是:SolutionName.ClassData 32次(结果数)

defaultly, the listbox displays the result of ToString() of the items you provided, which is 'SolutionName.ClassData' like you said. 默认情况下,列表框显示您提供的项目的ToString()的结果,即您所说的'SolutionName.ClassData'。
To change that, you need to change the DisplayMember property of the listbox- listBox.DisplayMember = "Name" 要更改此设置,您需要更改列表listBox.DisplayMember = "Name"DisplayMember属性listBox.DisplayMember = "Name"
for asp.net listbox, it's DataTextField 对于asp.net列表框,它是DataTextField

 <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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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