简体   繁体   中英

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. In the Default.aspx page I have:

<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. 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. However, why create the LinkButton dynamically in code? 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:

<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. 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. This results in much cleaner code. Instead of a ListView, you might also consider binding to a Repeater. ListViews are typically for two way binding directly to a database, but we're binding to a custom IEnumerable.

Also, if you do find that this markup is significantly cleaner, you might consider looking into 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. Instead of using a repeater, you'd just use a foreach loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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