简体   繁体   English

将列表绑定到ListView

[英]Bind a List to a ListView

I am simply trying to create a list and add elements to it from the code behind. 我只是试图创建一个列表,并从后面的代码向其中添加元素。 Each list element must be connected to a function in the code behind so I am using the Asp:LinkButton to do this. 每个列表元素都必须连接到后面代码中的函数,因此我正在使用Asp:LinkBut​​ton来执行此操作。 In the Default.aspx page I have: 在Default.aspx页中,我有:

<asp:ListView ID="ulNumTenants" runat="server">
    <ItemTemplate>
        <li>
        <%# DataBinder.Eval(Container.DataItem, "XXX" ) %>
        </li>
    </ItemTemplate>
 </asp:ListView>

And in the code behind I have the following: 在后面的代码中,我有以下内容:

var listItems = new List<LinkButton>();
int numberOfTenantsPossible = Space.MaxNumberOfTenants - (Space.MaleHousemates + Space.FemaleHousemates);
for (int itemCount = 0; itemCount < numberOfTenantsPossible; itemCount++ )
{
    LinkButton currentItem = new LinkButton();
    currentItem.CommandArgument = (itemCount + 1).ToString();
    currentItem.CommandName = "Tenant_OnClick";
    currentItem.Text = (itemCount + 1).ToString() + " tenants";
    listItems.Add(currentItem);
 }
 ulNumTenants.DataSource = listItems;
 ulNumTenants.DataBind();

The issue I am having is in the default.aspx code since I do not know what the expression field( "XXX" ) should be set to when I am not getting the entries from a database. 我遇到的问题在于default.aspx代码中,因为当我从数据库中获取条目时,我不知道应将表达式字段(“ XXX”)设置为什么。 Any suggestions are greatly appreciated. 任何建议,不胜感激。

Try this: 尝试这个:

<%# Container.DataItem  %>

I doubt it will work, since I think it will just take the string representation of a LinkButton instead of the HTML markup. 我怀疑它是否会起作用,因为我认为它将仅使用LinkBut​​ton的字符串表示形式而不是HTML标记。 However, why create the LinkButton dynamically in code? 但是,为什么要在代码中动态创建LinkBut​​ton? Try this instead: 尝试以下方法:

Code Behind: 背后的代码:

public class TenantViewModel
{
   public string ID {get; set;}
   public string Name {get; set;}
}

int numberOfTenantsPossible = Space.MaxNumberOfTenants - (Space.MaleHousemates + Space.FemaleHousemates);
var vms = new List<TenantViewModel>();
for (int itemCount = 0; itemCount < numberOfTenantsPossible; itemCount++ )
{
    var vm = new TenantViewModel { ID = (itemCount + 1).ToString(), Name = (itemCount + 1).ToString() + " tenants"};
    vms.Add(vm);
}
ulNumTenants.DataSource = vms;
ulNumTenants.DataBind();

ASPX: ASPX:

<asp:ListView ID="ulNumTenants" runat="server">
    <ItemTemplate>
        <li>
            <asp:LinkButton runat="server" CommandName="Tenant_OnClick" CommandArgument='<%# (Container.DataItem as TenantViewModel).ID' Text='<%# (Container.DataItem as TenantViewModel).Name' />
        </li>
    </ItemTemplate>
 </asp:ListView>

That allows you to keep UI element declaration in your ASPX markup, and instead of creating all the buttons in your code behind, you just create a view model to bind it to. 这样一来,您可以将UI元素声明保留在ASPX标记中,而无需在代码中创建所有按钮,只需创建一个视图模型即可将其绑定到。 Container.DataItem will be an object , so we use the as syntax to convert it to the correct type TenantViewModel so we can access the properties. Container.DataItem将是一个object ,因此我们使用as语法将其转换为正确的TenantViewModel类型,以便我们可以访问属性。 This results in much cleaner code. 这样可以使代码更简洁。 Instead of a ListView, you might also consider binding to a Repeater. 除了ListView,您还可以考虑绑定到Repeater。 ListViews are typically for two way binding directly to a database, but we're binding to a custom IEnumerable. ListView通常用于直接绑定到数据库的两种方式,但是我们绑定到自定义IEnumerable。

Also, if you do find that this markup is significantly cleaner, you might consider looking into ASP.NET MVC. 另外,如果您发现此标记明显更干净,则可以考虑研究ASP.NET MVC。 The markup gets even cleaner there with Razor syntax, because you won't have to worry about casting to the correct type. 使用Razor语法,标记变得更加清晰,因为您不必担心转换为正确的类型。 Instead of using a repeater, you'd just use a foreach loop. 无需使用转发器,只需使用foreach循环即可。

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

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