手动插入一项ListView控件 [英] Manually insert an item into a ListView control

查看:136
本文介绍了手动插入一项ListView控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜查S / O和谷歌,但我不能算出这个(大部分的搜索结果从一个数据源填充一个ListView)。我想一个项目手动添加到ListView控件,根据用户的选择。

I have searched S/O and google, but I can't figure this out (most of the search results as populating a Listview from a datasource). I want to add an item to a listview control manually, based on user selection.

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

我得到一个错误:结果
作为最佳重载方法匹配'System.Collections.Generic.ICollection.Add(System.Web.UI.WebControls.ListViewDataItem)'有一些无效参数

是什么原因造成的错误?

What is causing the error?

推荐答案

此错误只是意味着 lstAuthors [I] 不是<一href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewdataitem.aspx\"><$c$c>System.Web.UI.WebControls.ListViewDataItem (这是 ListView.Items.Add 功能的唯一有效的参数。

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.

在为了做到这一点你现在正在做的方式,你需要<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.listviewdataitem.listviewdataitem.aspx\">initialize一个ListViewDataItem ,并使用虚拟值的dataIndex参数(因为你没有一个基本的索引数据源):

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

说实话,这并没有真正看起来像使用的ListView 控制的正确途径。也许你能告诉我们你想完成什么,我们可以帮忙的另一种方法。

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.

下面是一个真正的下调,基本方法是做你想要做的事。你基本上保持一个通用的列表&LT; T&GT; 为您的数据源和绑定的的到你的的ListView 。这样,您就可以处理所有的维护您的ListView的内容的细节,但你仍然可以使用内置的数据绑定的能力。

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.

标记基础(有一个项目在它的ItemTemplate,一个DropDownList来选择项目,并添加这些项目到ListView一个按钮一个ListView):

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>

和code-背后:

// 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;
    }
}

这篇关于手动插入一项ListView控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆