我正在做一个学校项目,整个网站中只有一个功能遇到了麻烦。由于某些原因,当我单击“添加到购物车”链接时,即使我的所有插入参数在gridview表格行中都不为空,也将它们插入为空。我需要将商品的sku和价格插入到我的商品列表页面的购物车表格中,该页面以格式化的gridview显示所有带有图片的商品。我正在使用C#,ASP.net,HTML和CSS进行编码。我的数据库是在MySQL中设计的。
我已经玩了几天,阅读了各种资源,并尝试了有关代码项目的不同示例以及有关StackOverflow的其他问题的潜在解决方案。但是,没有任何东西可以解决我遇到的问题。我尝试通过链接按钮手动插入参数。似乎没有任何作用。我不知道怎么了。
网页:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ShopHome.aspx.cs" Inherits="WebApplication1.App_Pages.ShopHome" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Shop</title>
<!-- Nav-bar required: -->
<link rel="stylesheet" href="assets/css/Navigation-with-Button.css"/>
<link rel="stylesheet" href="assets/bootstrap/css/bootstrap.min.css"/>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<section>
<div id="nav-placeholder">
</div>
<script>
$(function () {
$("#nav-placeholder").load("nav-user.html");
});
</script>
</section>
<br /><br /> <br /><br /> <br /><br />
<form id="form1" runat="server">
<asp:GridView ID="items" runat="server" DataSourceID="SqlDataSource1" AutoGenerateColumns="False" DataKeyNames="sku" ShowInsertButton ="false">
<Columns>
<asp:BoundField DataField="sku" HeaderText="sku"
SortExpression="sku" ReadOnly="true"/>
<asp:BoundField DataField="color" HeaderText="color"
SortExpression="color" />
<asp:BoundField DataField="item_name" HeaderText="item_name"
SortExpression="item_name" />
<asp:BoundField DataField="price" HeaderText="price"
SortExpression="price" />
<asp:ImageField DataImageUrlField="imgPath" HeaderText="Image" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Runat="server"
OnCommand= "GridView1_AddCart" CommandName="Insert">Add To Cart</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:starssparksConnectionString %>"
ProviderName="<%$ ConnectionStrings:starssparksConnectionString.ProviderName %>"
SelectCommand="SELECT * FROM Item"
InsertCommand="INSERT INTO Cart (item_sku,quantity,cost,user_id) Values (@sku,1,@price,@user_id)">
<InsertParameters>
<asp:Parameter Name="sku" />
<asp:Parameter Name="price" />
<asp:SessionParameter Name="user_id" sessionfield="user_id" />
</InsertParameters>
</asp:SqlDataSource>
</form>
</body>
<script src="assets/bootstrap/js/bootstrap.min.js"></script>
<script src="assets/js/theme.js"></script>
</html>
网页后端:
using System;
using WebApplication1.App_Code;
using MySql.Data.MySqlClient;
using System.Web.UI.WebControls;
namespace WebApplication1.App_Pages
{
public partial class ShopHome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["user"] == null)
{
Response.Redirect("login.aspx");
}
else
{
userACC person = new userACC();
person.username = Session["user"].ToString();
int roleNum = person.checkRole();
if (roleNum != 1)
{
Response.Redirect("login.aspx");
}
}
userACC p = new userACC();
p.username = Session["user"].ToString();
int temp = p.getID();
this.Session["user_id"] = temp;
}
protected void GridView1_AddCart(object sender, System.Web.UI.WebControls.CommandEventArgs e)
{
if (e.CommandName == "Insert")
{
SqlDataSource1.Insert();
}
}
}
}
相关表的数据库设置:
sku INT NOT NULL AUTO_INCREMENT,
color varchar(45),
item_name varchar(45),
price DECIMAL(10,2),
imgPath varchar(500),
PRIMARY KEY (sku)
);
create table Cart(
id INT NOT NULL AUTO_INCREMENT,
item_sku INT,
quantity INT,
cost DECIMAL(10,2),
user_id INT,
PRIMARY KEY (id),
FOREIGN KEY user_id (user_id) REFERENCES userACC (id),
FOREIGN KEY item_sku (item_sku) REFERENCES Item (sku)
);