所以我這邊的做法是,使用雙重的ListVew做到動態新增row與column
外層是 row的 ListView 製作,然後裡面層是在外層的DataItem裡面多放一層ListView
然後裡面層的資料可以用外面層的物件,寫法如下
<asp:ListView ID="lvRoom" runat="server" ItemPlaceholderID="itemPlaceHolderRoom">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolderRoom" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td style="width:10%">
<asp:HiddenField ID="HF_ROOM_ID" runat="server" Value='<%# Eval("ROOM_ID")%>' />
<%# Eval("ROOM_NAME")%>
</td>
<asp:ListView ID="lvPrice" runat="server" ItemPlaceholderID="itemPlaceHolderPrice" DataSource='<%# Eval("Price")%>'>
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolderPrice" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:HiddenField ID="HF_PRICE_ID" runat="server" Value='<%# Eval("PRICE_ID")%>' />
</ItemTemplate>
</asp:ListView>
<td></td>
</tr>
</ItemTemplate>
</asp:ListView>
<asp:ListView ID="lvRoom" runat="server" ItemPlaceholderID="itemPlaceHolderRoom">
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolderRoom" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td style="width:10%">
<asp:HiddenField ID="HF_ROOM_ID" runat="server" Value='<%# Eval("ROOM_ID")%>' />
<%# Eval("ROOM_NAME")%>
</td>
<asp:ListView ID="lvPrice" runat="server" ItemPlaceholderID="itemPlaceHolderPrice" DataSource='<%# Eval("Price")%>'>
<LayoutTemplate>
<asp:PlaceHolder ID="itemPlaceHolderPrice" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:HiddenField ID="HF_PRICE_ID" runat="server" Value='<%# Eval("PRICE_ID")%>' />
</ItemTemplate>
</asp:ListView>
<td></td>
</tr>
</ItemTemplate>
</asp:ListView>
{ <asp:listview id="lvRoom" itemplaceholderid="itemPlaceHolderRoom" runat="server"> <layouttemplate> <asp:placeholder id="itemPlaceHolderRoom" runat="server"></asp:placeholder> </layouttemplate> <itemtemplate> <asp:hiddenfield id="HF_ROOM_ID" runat="server" value="<%# Eval("ROOM_ID")%>"> </asp:hiddenfield><br /> <asp:listview datasource="<%# Eval("Price")%>" id="lvPrice" itemplaceholderid="itemPlaceHolderPrice" runat="server"> <layouttemplate> <asp:placeholder id="itemPlaceHolderPrice" runat="server"></asp:placeholder> </layouttemplate> <itemtemplate> <asp:hiddenfield id="HF_PRICE_ID" runat="server" value="<%# Eval("PRICE_ID")%>"> </asp:hiddenfield></itemtemplate> </asp:listview> </itemtemplate> </asp:listview> }
重點是裡面那層的ListView中的Price物件
public class Price { public string PRICE_ID { get; set; } public string PRICE_NAME { get; set; } public int PRICE { get; set; } public int ADDPRICE_MAN { get; set; } public int ADDPRICE_BED { get; set; } } public class Room { public string ROOM_ID { get; set; } public string ROOM_NAME { get; set; } public ListPrice { get; set; } public Room() { Price = new List (); } }