asp.net ListView控件的简单实用和配置

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>

 这个控件包含了几个不同的模板,效果如下:

 

posted @ 2017-09-28 13:45  白羽轻飘  阅读(5328)  评论(1编辑  收藏  举报