0

任何人都可以指导我在列表视图中动态生成列,就像我们可以在 ASP.net 中为 GridView 做的那样吗?

4

1 回答 1

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>

谢谢

迪普

于 2012-04-04T09:07:29.113 回答