Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 具有自动生成列的Listview_Asp.net_Listview - Fatal编程技术网

Asp.net 具有自动生成列的Listview

Asp.net 具有自动生成列的Listview,asp.net,listview,Asp.net,Listview,有谁能指导我在listview中动态生成列,就像我们在ASP.net中为GridView所做的那样?listview控件不存在这样的属性。不必对页面中的列进行硬编码,您可以像下面这样从代码隐藏中生成这些列 class Movie { public string Title {get;set;} } ListView1.DataSource = new List<Movie>() {

有谁能指导我在listview中动态生成列,就像我们在ASP.net中为GridView所做的那样?

listview控件不存在这样的属性。不必对页面中的列进行硬编码,您可以像下面这样从代码隐藏中生成这些列

       class Movie
       {
          public string Title {get;set;}
       }

        ListView1.DataSource = new List<Movie>()
        {
               new Movie {Title = "tEST"},
               new Movie {Title = "tEST"},
               new Movie {Title = "tEST"},
               new Movie {Title = "tEST"}

        };

        ListView1.DataBind();

        var columns = new string[]
        {
           "Coulmn 1",
           "Coulmn 2",
           "Coulmn 3"
        };

        var row = ListView1.FindControl("header") as HtmlTableRow;

        if (row != null)
        {
            foreach (var column in columns)
            {
                HtmlTableCell cell = new HtmlTableCell();
                cell.InnerText = column;
                row.Cells.Add(cell);
            }
        }

      <asp:ListView runat="server" ID="ListView1"  >
      <LayoutTemplate>
       <table runat="server" id="table1" >
        <tr id="header" runat="server">
        </tr>
       <tr runat="server" id="itemPlaceholder" ></tr>
      </table>
     </LayoutTemplate>
     <ItemTemplate>
     <tr>
      <td>
          <asp:Label ID="NameLabel" runat="server" 
           Text='<%#Eval("Title") %>' />
      </td>
    </tr>
     </ItemTemplate>
    </asp:ListView>
谢谢


Deepu

ListView不支持此类属性。为什么不想使用GridView?@Helper GridView不支持插入数据,只是显示和编辑数据,不能100%确定这是原因,但这是一个有效的原因!