Listview with autogenerate columns

时间:2012-04-04 08:28:36

标签: asp.net listview

有人可以指导我在listview中动态生成列,就像我们可以在ASP.net中为GridView做的那样吗?

1 个答案:

答案 0 :(得分:0)

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>

由于

迪普