3

我已经搜索了 S/O 和 google,但我无法弄清楚(大多数搜索结果都是从数据源填充 Listview)。我想根据用户选择手动将项目添加到列表视图控件。

ListView listView1 = new ListView();
listView1.Items.Add(lstAuthors[i]);  

我收到一个错误:
“System.Collections.Generic.ICollection.Add(System.Web.UI.WebControls.ListViewDataItem)”的最佳重载方法匹配有一些无效参数

是什么导致了错误?

4

1 回答 1

7

This error simply means that lstAuthors[i] is not a System.Web.UI.WebControls.ListViewDataItem (which is the only valid parameter for the ListView.Items.Add function.

In order to do this the way you are doing it now, you would need to initialize a ListViewDataItem, and use dummy values for the dataIndex parameter (since you don't have an underlying indexed datasource):

ListViewDataItem newItem = new ListViewDataItem(dataIndex, displayIndex);

To be honest, this doesn't really seem like the right way to use the ListView control. Maybe you could tell us what you're trying to accomplish, and we could help out with another approach.


Here is a really trimmed down, basic approach to doing what you would like to do. You basically maintain a generic List<T> as your datasource, and bind that to your ListView. This way you can handle all of the details of maintaining the contents of your ListView, but you can still use the built in power of the databinding.

Basic markup (a ListView with one item in it's ItemTemplate, a DropDownList to select items from, and a Button for adding those items to the ListView):

<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ListView ID="ListView1" runat="server">
        <ItemTemplate>
            <div>
                <asp:Label ID="AuthorNameLbl" runat="server" Text='<%# Eval("AuthorName") %>'></asp:Label>
            </div>
        </ItemTemplate>
    </asp:ListView>
    <br />
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>Stephen King</asp:ListItem>
        <asp:ListItem>Mary Shelley</asp:ListItem>
        <asp:ListItem>Dean Koontz</asp:ListItem>
    </asp:DropDownList>
    <br />
    <br />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
</asp:Content>

And code-behind:

// Honestly, this string  just helps me avoid typos when 
// referencing the session variable
string authorKey = "authors";

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        // If the session variable is empty, initialize an 
        // empty list as the datasource
        if (Session[authorKey] == null)
        {
            Session[authorKey] = new List<Author>();
        }
        BindList();
    }
}

protected void Button1_Click(object sender, EventArgs e)
{
    // Grab the current list from the session and add the 
    // currently selected DropDown item to it.
    List<Author> authors = (List<Author>)Session[authorKey];
    authors.Add(new Author(DropDownList1.SelectedValue));
    BindList();
}

private void BindList()
{
    ListView1.DataSource = (List<Author>)Session[authorKey];
    ListView1.DataBind();
}

// Basic author object, used for databinding
private class Author
{
    public String AuthorName { get; set; }

    public Author(string name)
    {
        AuthorName = name;
    }
}
于 2013-03-14T20:55:34.963 回答