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>

 這個控件包含了幾個不同的模板,效果如下:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM