0

我想在代码中将 Prd_id 发布到网页 Products.aspx

<asp:DataList ID="DataList1" runat="server"
         DataKeyField="Prd_id" DataSourceID="AccessDataSource1">
         <ItemTemplate>
            <a href='Products.aspx?prdid=<%# Eval("Prd_id") %>'>Add to Cart</a>
         </ItemTemplate>
</asp:DataList>
4

1 回答 1

0

尝试使用隐藏字段来存储选定的产品 ID:

HTML:

    <script type="text/javascript">
        function addProduct(prid) {
            // assign the product ID to the hidden field's value
            document.getElementById("<%= hidProductId.ClientID %>").value = prid;
        }
    </script>

    <asp:DataList ID="DataList1" runat="server" DataKeyField="Prd_id" DataSourceID="AccessDataSource1">
        <ItemTemplate>
            <asp:LinkButton ID="btnAdd" runat="server" OnClick="btnAdd_Click" OnClientClick="<%# "addProduct('" + Eval("Prd_id") + "');" %>" ></asp:LinkButton>
        </ItemTemplate>
    </asp:DataList>

    <asp:HiddenField ID="hidProductId" runat="server" />

后面的代码:

    protected void btnAdd_Click(object sender, EventArgs e)
    {
        var productId = hidProductId.Value;
    }
于 2013-01-15T02:25:29.323 回答