0
我正在ASP Webforms中構建電子商務網站。我將產品列表顯示爲從SQL Serer獲取數據的listview。我在列表視圖的項目模板中有一個文本框,用戶可以在其中輸入產品的數量。我想動態地將ID設置爲Control ID,這樣我就可以在後面的代碼中檢索到數量。如何動態地將ID添加到文本框中並在代碼中將其放回到Webforms中
bio.aspx
<ItemTemplate>
<td runat="server" style="background-color:#DCDCDC;color: #000000;">
<asp:Image ImageUrl='<%# Eval("Image") %>' ID ="ImageLable" runat="server" CssClass="productImage" />
<br />
<asp:Label ID="Product_NameLabel" runat="server" Text='<%# Eval("[Product Name]") %>' />
<br />Price:
<asp:Label ID="PriceLabel" runat="server" Text='<%# Eval("Price") %>' />
<br />
<asp:TextBox ID='TextBox1' runat="server"></asp:TextBox>
</br>
<asp:Button ID="ButtonClick" runat="server" Text="Add To Cart" CommandArgument='<%# Eval("Id") %>' OnClick="ButtonClick_Click"/>
</td>
</ItemTemplate>
bio.cs
protected void ButtonClick_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
string buttonId = button.CommandArgument.ToString();
int id = Convert.ToInt32(buttonId);
String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["2016_675_z1787626ConnectionString"].ConnectionString;
using (SqlConnection conn = new SqlConnection(connectionString))
{
String command = "Insert into Cart (ProductId,ProductQty,UserId) Values(@ProductId,@ProductQ,1)";
conn.Open();
SqlCommand cmd = new SqlCommand(command, conn);
cmd.Parameters.Add("@ProductId", System.Data.SqlDbType.Int).Value = id;
cmd.Parameters.Add("@ProductQ", System.Data.SqlDbType.Int).Value = 1;
cmd.ExecuteNonQuery();
}
Response.Redirect("cart.aspx");
}
我想添加一個文本框輸入的每個產品列表的數量,所以當我點擊addToCart鍵,標識以及產品質量需要更新到數據庫 –
我更新答案。 – Win
但只有第一個產品具有Id「TextBox1」。我有近20個產品通過列表視圖顯示 –