将Eval值传递给购物车的C#方法

本文关键字:方法 购物车 Eval 值传 | 更新日期: 2023-09-27 17:59:39

我正在为我的班级创建一个购物车web应用程序。当用户单击add to cart按钮时,我想将每个特定产品的ID的值传递给后面代码中的一个单独方法。不确定我的语法是错误的,还是这不起作用。这是我的标记:

<asp:Button ID="AddToCart" CommandName="Add" 
OnClientClick ='<%# ListView1_AddToCart(Eval("ID"))%>'
CssClass ="Button" runat="server" Text="Add to Cart" />

下面是我的代码:

public void ListView1_AddToCart(string CatID)
{cart.AddToCart(CatID);}

我不断收到各种问题,但这给了我以下错误:

The best overloaded method match for 'OurCats_GrumpyCats.ListView1_AddToCart(string)'
has some invalid arguments.

我该如何解决此问题?有更好的方法吗?

编辑:这是我的标记

    <%@ Page Title="All Cats" Language="C#" MasterPageFile="~/MasterPage/Layout.master"        AutoEventWireup="true" CodeFile="AllCats.aspx.cs" Inherits="OurCats_GrumpyCats" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cpMainContent" Runat="Server">
    <h1>Meet all of our kittens!</h1>
    <asp:ListView ID="ListView1" runat="server" DataKeyNames="ID" DataSourceID="SqlDataSource2">


        <EmptyDataTemplate>
            <span>No data was returned.</span>
        </EmptyDataTemplate>
        <InsertItemTemplate>
            <span style="">ID:
            <asp:TextBox ID="IDTextBox" runat="server" Text='<%# Bind("ID") %>' />
            <br />
            Name:
            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
            <br />
            Price:
            <asp:TextBox ID="PriceTextBox" runat="server" Text='<%# Bind("Price") %>' />
            <br />
            Imgu:
            <asp:TextBox ID="ImguTextBox" runat="server" Text='<%# Bind("Img") %>' />
            <br />
            Description:
            <asp:TextBox ID="DescriptionTextBox" runat="server" Text='<%# Bind("Description") %>' />
            <br />
            <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" />
            <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" />
            <br />
            <br />
            </span>
        </InsertItemTemplate>
        <ItemTemplate>
        <table class ="Table" style="border-style: solid; ">
         <tr>
                <td >
                    <a href ="Details.aspx?ID=<%# Eval("ID")%>">
                        <img src="../Images/<%# Eval("Img") %>" width ="200"  />
              </a>
                </td>
                <td style="width:700px; margin-left: 100px">
                    <asp:Label ID="Label1" runat="server" Text="Name: "></asp:Label><asp:Label ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' />
                    <asp:Textbox ID="CatID" runat="server" Visible="false" Text='<%# Eval("ID")%>'></asp:Textbox>
                    <br />
                    <br />
                    <asp:Label ID="Label2" runat="server" Text="Price: $"></asp:Label><asp:Label ID="PriceLabel" runat="server" Text= '<%# Eval("Price") %>'/>
                <br />
                    <br />
                    <asp:Label ID="Label3" runat="server" Text="Description: "></asp:Label><asp:Label ID="DescriptionLabel" runat="server" Text= '<%# Eval("Description") %>' />
                    <br />
                    <br />
                    <asp:Button ID="AddToCart" CommandName="Add" OnClientClick='<%# "AddToCart(" +Eval("ID") + " );" %>' CssClass ="Button" runat="server" Text="Add to Cart" />
                </td>
            </tr>
           </table> 

        </ItemTemplate>
        <LayoutTemplate>
            <div id="itemPlaceholderContainer" runat="server" style =" margin-left :30px;">
                <span runat="server" id="itemPlaceholder" />
            </div>
            <div style="">
            </div>
        </LayoutTemplate>
    </asp:ListView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:420_Project_GLConnectionString1 %>" SelectCommand="SELECT * FROM [OurCats]">
    </asp:SqlDataSource>
</asp:Content>

将Eval值传递给购物车的C#方法

尝试以下代码

<asp:Button ID="AddToCart" CommandName="Add" 
OnClientClick ='<%# ListView1_AddToCart((string)Eval("ID"))%>'
CssClass ="Button" runat="server" Text="Add to Cart" />
OnClientClick ='<%# "ListView1_AddToCart(" +Eval("ID") + " );" %>'