首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何枚举ASP列表视图控件?

如何枚举ASP列表视图控件?
EN

Stack Overflow用户
提问于 2018-05-22 21:56:56
回答 2查看 35关注 0票数 0

下面我将获取一个列表,将其转换为数据表,并将其绑定到ASP listview控件。

我想要一个函数来将它从asp listview控件转换回一个列表,但不知道如何获取这些项?在Visual Studio调试中,数据项是否为空?它有正确的计数但没有值吗?我想枚举所有行。

   private void createLvwTable(List<string> table)
    {
        int index = 0;
        DataTable dt = new DataTable();
        foreach (string row in table)
        {
            string[] cells = row.Split('|');
            if (index == 0) // header column
            {
                for (int j = 0; j < cells.Length; j++)
                {
                    dt.Columns.Add(cells[j]);
                    //dt.Rows.Add();
                }
            }
            else
            {
                DataRow dr = dt.NewRow();
                for (int j = 0; j < cells.Length; j++)
                {
                    dr[j] = cells[j];
                }
                dt.Rows.Add(dr);
            }
            index++;
        }
        lvwOutput.DataSource = dt;
        lvwOutput.DataBind();
    }
EN

回答 2

Stack Overflow用户

发布于 2018-05-22 23:17:56

这是愚蠢的,所以很可能有一种更好的方法,但似乎您必须在列表视图创建过程中将数据绑定到对象。我在任何地方都找不到好的答案,这是几个小时的搜索和尝试半相关答案的不同组合的汇编。

在HTML端,你必须将'onitemdatabound‘设置为一个c#函数。下面的代码也不需要我使用"|“的分段,我把它留在里面是为了让你在复制/粘贴我的函数时更容易阅读。

我仍然很高兴看到关于如何做得更好的回复,这样我就可以学习了。

html:
<asp:ListView ID="lvwOutput" runat="server" onitemdatabound="lvwOutput_ItemDataBound">


asp:
           private List<string> lvwOutputItemsDataBoundToList = new List<string>();
    private List<string> lvwOutputItemsDataBoundToListOriginal = new List<string>();
    protected void lvwOutput_ItemDataBoundToList(object sender, ListViewItemEventArgs e)
    {
        if (e.Item.ItemType == ListViewItemType.DataItem)
        {
            ListViewDataItem dataItem = (ListViewDataItem)e.Item;
            object o = (object)dataItem.DataItem;
            System.Data.DataRowView rowView = e.Item.DataItem as System.Data.DataRowView;
            object[] itemArray = rowView.Row.ItemArray;

            string itemBound = "";
            foreach(object item in itemArray)
            {
                itemBound += item.ToString() + "|";
            }

            if (itemBound.EndsWith("||"))
            {
                int index = itemBound.Length;
                itemBound = itemBound.Remove(index - 2);
            }

            if (itemBound.EndsWith("|"))
            {
                int index = itemBound.Length;
                itemBound = itemBound.Remove(index - 1);
            }

            lvwOutputItemsDataBoundToList.Add(itemBound);
        }

        ViewState["lvwOutputItemsDataBoundToList"] = lvwOutputItemsDataBoundToList;

    }



private void filter()
    {
        lvwOutputItemsDataBoundToList = (List<string>)ViewState["lvwOutputItemsDataBoundToList"];
        lvwOutputItemsDataBoundToListOriginal = lvwOutputItemsDataBoundToList;

        foreach (string item in lvwOutputItemsDataBoundToList)
        {
            string[] itemSplit = item.Split('|');
        }


    }
票数 0
EN

Stack Overflow用户

发布于 2018-06-05 05:45:20

要枚举ListViewItems,请参阅ListView.ListViewItemCollection ClassListViewItem.SubItems Property

List<string> lstItems = new List<string>():
foreach(ListViewItem itemRow in lvwOutput)
{
    for (int i = 0; i < itemRow.SubItems.Count; i++)
    {
        lstItems.Add(itemRow.SubItems[i].Text);
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50469562

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档