如何在“添加到购物车”按钮上编码

Posted

技术标签:

【中文标题】如何在“添加到购物车”按钮上编码【英文标题】:How to code on "add to cart" button 【发布时间】:2012-11-29 03:55:24 【问题描述】:

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

    <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>

【问题讨论】:

不需要额外的东西,尤其是在你的问题标题中。 我不能给你源代码,但你可以使用控件的 itemcommand 事件,它会达到目的。 请说明过程... 请参考msdn.microsoft.com/en-us/library/… 【参考方案1】:

将命令与按钮和列表视图 ItemCommand 事件一起使用。

aspx 页面:

<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
        
    

【讨论】:

以上是关于如何在“添加到购物车”按钮上编码的主要内容,如果未能解决你的问题,请参考以下文章

如何在添加到购物车按钮旁边移动数量输入字段 - wordpress

Woocommerce我如何将添加到购物车按钮集中在单个页面上

Woocommerce Astra 主题显示两个添加到购物车按钮

如何对循环内的按钮使用相同的功能?

替换产品类别的 Woocommerce 单一产品页面上的“添加到购物车”按钮

vue中如何根据库存将产品添加到购物车