如何使用 link 中的值?
How do I use the value in the link?
我不确定如何提出这个问题,我试图寻找解决方案,但没有成功,因为我不知道要搜索什么...所以我有一个 link 按钮来使用产品 ID 将用户引导至 addtocart.aspx,link 应如下所示
addtocart.aspx/?id=1
我的问题是如何使用 link 中的 id 值将用户重定向到并将其保存到购物车的另一个数据库。这是我目前的进展,但仍然无法显示 id(此代码在 addtocart.aspx 的页面加载下)
if (!IsPostBack)
{
String contact_id = Id;
int intTest = Convert.ToInt32(contact_id);
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Product WHERE Id=" + intTest))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string id = row["Id"].ToString();
string Product_Name = row["Product_Name"].ToString();
string Product_Price = row["Product_Price"].ToString();
string username = Session["Username"].ToString();
con.Close();
con.Open();
string query = "INSERT INTO Cart (Guest_Id, Product_Name, Product_Price) values (@Guest_Id, @Product_Name, @Product_Price)";
SqlCommand cmd1 = new SqlCommand(query, con);
cmd1.Parameters.AddWithValue("@Username", username);
cmd1.Parameters.AddWithValue("@Product_Name", Product_Name);
cmd1.Parameters.AddWithValue("@Product_Price", Product_Price);
cmd1.ExecuteNonQuery();
Response.Redirect("Dining.aspx");
con.Close();
}
}
}
}
}
}
要获取查询字符串参数,只需使用 Request.QueryString
。所以,
// this gets the 'id' parameter from rthe query string, as a string
string contact_id = Request.QueryString["id"];
我不确定如何提出这个问题,我试图寻找解决方案,但没有成功,因为我不知道要搜索什么...所以我有一个 link 按钮来使用产品 ID 将用户引导至 addtocart.aspx,link 应如下所示
addtocart.aspx/?id=1
我的问题是如何使用 link 中的 id 值将用户重定向到并将其保存到购物车的另一个数据库。这是我目前的进展,但仍然无法显示 id(此代码在 addtocart.aspx 的页面加载下)
if (!IsPostBack)
{
String contact_id = Id;
int intTest = Convert.ToInt32(contact_id);
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Product WHERE Id=" + intTest))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
string id = row["Id"].ToString();
string Product_Name = row["Product_Name"].ToString();
string Product_Price = row["Product_Price"].ToString();
string username = Session["Username"].ToString();
con.Close();
con.Open();
string query = "INSERT INTO Cart (Guest_Id, Product_Name, Product_Price) values (@Guest_Id, @Product_Name, @Product_Price)";
SqlCommand cmd1 = new SqlCommand(query, con);
cmd1.Parameters.AddWithValue("@Username", username);
cmd1.Parameters.AddWithValue("@Product_Name", Product_Name);
cmd1.Parameters.AddWithValue("@Product_Price", Product_Price);
cmd1.ExecuteNonQuery();
Response.Redirect("Dining.aspx");
con.Close();
}
}
}
}
}
}
要获取查询字符串参数,只需使用 Request.QueryString
。所以,
// this gets the 'id' parameter from rthe query string, as a string
string contact_id = Request.QueryString["id"];