2

我正在使用列表视图,请告诉我如何从列表视图模板中选择特定项目。在列表视图模板中,我使用了一个名为“添加到购物车”的按钮。以及如何将产品添加到购物车页面。

    <asp:ListView ID="ListView1" runat="server" DataSourceID="SqlDataSource1" 
    GroupItemCount="5">
    <AlternatingItemTemplate>
        <td runat="server" style="background-color:#FFF8DC;">
            <asp:Image ID="Image2" runat="server" Height="101px" 
                ImageUrl='<%# Eval("img") %>' Width="100px" />
            <br />
            Title:
            <asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>' />
            <br />author:
            <asp:Label ID="authorLabel" runat="server" Text='<%# Eval("author") %>' />
            <br />publisher:
            <asp:Label ID="publisherLabel" runat="server" Text='<%# Eval("publisher") %>' />
            <br />price:
            <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
            <br />
            <asp:Button ID="Button4" runat="server" Text="Add to Cart" 
                OnClick="Button4_Click" PostBackUrl="~/ShoppingCart.aspx" />
            <br />
        </td>
    </AlternatingItemTemplate>
    <EditItemTemplate>
        <td runat="server" style="background-color:#FFF8DC;">
            <asp:Image ID="Image2" runat="server" Height="101px" 
                ImageUrl='<%# Eval("img") %>' Width="100px" />
            <br />
            Title:
            <asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>'></asp:Label>
            <br />author:
            <asp:Label ID="authorLabel" runat="server" Text='<%# Eval("author") %>'></asp:Label>
            <br />publisher:
            <asp:Label ID="publisherLabel" runat="server" Text='<%# Eval("publisher") %>'></asp:Label>
            <br />price:
            <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>'></asp:Label>
            <br />
            <asp:Button ID="Button4" runat="server" 
                Text="Add to Cart" OnClick="Button4_Click" 
                OnClientClick='<%# Eval("title", "{0}") %>' PostBackUrl="~/ShoppingCart.aspx" />
            <br />
        </td>
    </EditItemTemplate>
    <EmptyDataTemplate>
        <table runat="server" 
            style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;">
            <tr>
                <td>
                    No data was returned.</td>
            </tr>
        </table>
    </EmptyDataTemplate>
    <EmptyItemTemplate>
<td runat="server" />
    </EmptyItemTemplate>
    <GroupTemplate>
        <tr ID="itemPlaceholderContainer" runat="server">
            <td ID="itemPlaceholder" runat="server">
            </td>
        </tr>
    </GroupTemplate>
    <InsertItemTemplate>
        <td runat="server" style="">
            title:
            <asp:TextBox ID="titleTextBox" runat="server" Text='<%# Bind("title") %>' />
            <br />author:
            <asp:TextBox ID="authorTextBox" runat="server" Text='<%# Bind("author") %>' />
            <br />publisher:
            <asp:TextBox ID="publisherTextBox" runat="server" 
                Text='<%# Bind("publisher") %>' />
            <br />price:
            <asp:TextBox ID="priceTextBox" runat="server" Text='<%# Bind("price") %>' />
            <br />img:
            <asp:TextBox ID="imgTextBox" runat="server" Text='<%# Bind("img") %>' />
            <br />
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" 
                Text="Insert" />
            <br />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" 
                Text="Clear" />
            <br />
        </td>
    </InsertItemTemplate>
    <ItemTemplate>
        <td runat="server" style="background-color:#DCDCDC;color: #000000;">
            <asp:Image ID="Image1" runat="server" Height="100px" 
                ImageUrl='<%# Eval("img") %>' Width="101px" />
            <br />
            Title:
            <asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>' />
            <br />Author:
            <asp:Label ID="authorLabel" runat="server" Text='<%# Eval("author") %>' />
            <br />Publisher:
            <asp:Label ID="publisherLabel" runat="server" Text='<%# Eval("publisher") %>' />
            <br />Price:
            <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
            <br />
            <asp:Button ID="Button3" runat="server" Text="Add to Cart" 
                OnClientClick='<%# Eval("title", "{0}") %>' />
            <br />
        </td>
    </ItemTemplate>
    <LayoutTemplate>
        <table runat="server">
            <tr runat="server">
                <td runat="server">
                    <table ID="groupPlaceholderContainer" runat="server" border="1" 
                        style="background-color: #FFFFFF;border-collapse: collapse;border-color: #999999;border-style:none;border-width:1px;font-family: Verdana, Arial, Helvetica, sans-serif;">
                        <tr ID="groupPlaceholder" runat="server">
                        </tr>
                    </table>
                </td>
            </tr>
            <tr runat="server">
                <td runat="server" 
                    style="text-align: center;background-color: #CCCCCC;font-family: Verdana, Arial, Helvetica, sans-serif;color: #000000;">
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <SelectedItemTemplate>
        <td runat="server" 
            style="background-color:#008A8C;font-weight: bold;color: #FFFFFF;">
            title:
            <asp:Label ID="titleLabel" runat="server" Text='<%# Eval("title") %>' />
            <br />author:
            <asp:Label ID="authorLabel" runat="server" Text='<%# Eval("author") %>' />
            <br />publisher:
            <asp:Label ID="publisherLabel" runat="server" Text='<%# Eval("publisher") %>' />
            <br />price:
            <asp:Label ID="priceLabel" runat="server" Text='<%# Eval("price") %>' />
            <br />img:
            <asp:Label ID="imgLabel" runat="server" Text='<%# Eval("img") %>' />
            <br />
        </td>
    </SelectedItemTemplate>

</asp:ListView>
4

1 回答 1

1

将 Command 与 Button 和 Listviews ItemCommand 事件一起使用。

页面:

<asp:ListView ID="ListView1" runat="server" DataSourceID="AccessDataSource1" 
    GroupItemCount="3"  ItemPlaceholderID="itemPlaceholder" DataKeyNames="Id">

  <asp:Button ID="cartbtn" Text="Add To cart" runat="server" ForeColor="Blue" Font-Underline="true" CommandName="Add" ></asp:Button>

    </ListView>

在后面的代码中:

      protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
    {       
        if (e.CommandName.ToString()=="Add")
        {
           //Write your code Here
        }
    }
于 2012-11-29T05:40:56.300 回答