1 web窗體界面代碼
ItemType:控件要綁定的實體模型
SelectMethod:控件獲取實體集合的后台方法
DataKeyNames:實體的主鍵
UpdateProduct:設置跟新的方法
DeleteMethod:刪除實體的方法
InsertProduct:插入實體的方法
InsertItemPosition:新插入實體的位置
EnableViewState:禁用視圖狀態
這些方法都可以在自定義在web窗體的代碼后置中
<asp:ListView runat="server" ItemType="SportsStore.Models.Product" SelectMethod="GetProducts" DataKeyNames="ProductID" UpdateMethod="UpdateProduct" DeleteMethod="DeleteProduct" InsertMethod="InsertProduct" InsertItemPosition="LastItem" EnableViewState="false"> <LayoutTemplate> <div class="outerContainer"> <table id="productsTable"> <tr> <th>Name</th> <th>Description</th> <th>Category</th> <th>Price</th> </tr> <tr runat="server" id="itemPlaceholder"> </tr> </table> </div> </LayoutTemplate> <ItemTemplate> <tr> <td><%#Item.Name %></td> <td class="description"> <span> <%#Item.Description %> </span> </td> <td><%#Item.Category %></td> <td><%#Item.Price.ToString("c") %></td> <td> <asp:Button CommandName="Edit" Text="Edit" runat="server" /> <asp:Button CommandName="Delete" Text="Delete" runat="server" /> </td> </tr> </ItemTemplate> <EditItemTemplate> <tr> <td> <input name="name" value="<%#Item.Name%>" /> <input type="hidden" name="ProductID" value="<%#Item.ProductID%>" /> </td> <td> <input name="description" value="<%#Item.Description %>" /> </td> <td> <input name="category" value="<%#Item.Category %>" /> </td> <td> <input name="price" value="<%#Item.Price %>" /> </td> <td> <asp:Button CommandName="Update" Text="Update" runat="server" /> <asp:Button CommandName="Cancel" Text="Cancel" runat="server" /> </td> </tr> </EditItemTemplate> <InsertItemTemplate> <tr> <td> <input name="name" /> <input type="hidden" name="ProductID" value="0" /> </td> <td> <input name="description" /> </td> <td> <input name="category" /> </td> <td> <input name="price" /> </td> <td> <asp:Button CommandName="Insert" Text="Add" runat="server"/> </td> </tr> </InsertItemTemplate> </asp:ListView>
這個控件包含了幾個不同的模板,效果如下: