目前我已經顯示在FormView控件我的產品詳細信息頁面看起來如下:添加到購物車中FormsView而不是DetailsView控件
我對添加到購物車按鈕,這個塊代碼,但它是爲DetailsView。如何修改代碼以適用於FormView?
protected void btnBuy_Click(Object sender, EventArgs e)
{
// test to remind customer to login first
if ((string)Session["sFlag"] != "T")
{
Type csType = this.GetType();
ClientScript.RegisterStartupScript(csType, "Error", scriptErrorLogin);
}
else
{
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
+ Server.MapPath("~/App_Data/TabStoreDB.mdb");
OleDbCommand cmd;
//insert item purchased to itemsTable
int intOrderNo = (int)Session["sOrderNo"];
DetailsViewRow row0 = DetailsView1.Rows[0]; // Rows[0] contains the productId
String strProductId = row0.Cells[1].Text;
DetailsViewRow row6 = DetailsView1.Rows[6]; // Rows[6] contains the unit price
String strUnitPrice = row6.Cells[1].Text;
float floUnitPrice = float.Parse(strUnitPrice);
String strQty = DropDownList1.Items[DropDownList1.SelectedIndex].ToString();
int intQty = int.Parse(strQty);
string strSQL = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
+ "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
cmd = new OleDbCommand(strSQL, mDB);
cmd.Parameters.AddWithValue("@OrderNO", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", strProductId);
cmd.Parameters.AddWithValue("@Qty", intQty);
cmd.Parameters.AddWithValue("@UnitPrice", floUnitPrice);
mDB.Open();
cmd.ExecuteNonQuery();
mDB.Close();
Response.Redirect("ShoppingCart.aspx");
}
}
FormView的代碼:
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ProductDetails" Runat="Server">
<asp:FormView ID="FormView_Product" runat="server" DataKeyNames="ProductID"
DataSourceID="AccessDataSource1" Height="296px" Width="223px"
style="margin-right: 0px">
<ItemTemplate>
<table id="Table_01" width="770" height="745" border="0" cellpadding="0" cellspacing="0">
<tr>
<td rowspan="5" align="center">
<img src='<%# Eval("ProductUrl") %>' width="238" height="243"
alt='<%# Eval("ProductModel") %>' /></td></td>
<td rowspan="7">
<img src="images/productdetails_03.jpg" width="1" height="705" alt=""></td>
<td rowspan="7" valign="top">
<br /><br /><%# Eval("ProductID") %><span class="productname-text"><%# Eval("ProductBrand") %> <%# Eval("ProductModel") %></span>
<br /> <span class="style1">Our Price:</span> <span class="price-text">S$<%# Eval("NormalPrice") %></span>
<br /><span class="description-text"><%# Eval("ProductDetails") %></span>
</td>
<td colspan="4">
<img src="images/productdetails_05.jpg" width="196" height="98" alt=""></td>
</tr>
<tr>
<td rowspan="2">
<img src="images/productdetails_06.jpg" width="1" height="42" alt=""></td>
<td colspan="3">
<img src="images/productdetails_07.jpg" width="195" height="41" alt=""></td>
</tr>
<tr>
<td colspan="3">
<img src="images/productdetails_08.jpg" width="195" height="1" alt=""></td>
</tr>
<tr>
<td colspan="2">
<img src="images/productdetails_09.jpg" width="7" height="94" alt=""></td>
<td background="images/productdetails_10.jpg" valign="top">
<br />
<span class="quantity-text"> Quantity:</span>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem Selected="True">1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
<asp:ListItem>5</asp:ListItem>
</asp:DropDownList>
<br />
<br />
<asp:ImageButton ID="btnBuy" runat="server"
ImageUrl="~/images/addtocartbutton.jpg" />
<td>
<img src="images/productdetails_11.jpg" width="7" height="94" alt=""></td>
</tr>
<tr>
<td colspan="4" rowspan="3">
<img src="images/productdetails_12.jpg" width="196" height="471" alt=""></td>
</tr>
<tr>
<td>
<img alt='<%# Eval("ProductModel") %>' border="0"
src='<%# Eval("ProductUrl") %>' style="height: 83px; width: 83px" /><img
alt='<%# Eval("ProductModel") %>' border="0" src='<%# Eval("ProductUrl") %>'
style="height: 83px; width: 83px" /><img alt='<%# Eval("ProductModel") %>'
border="0" src='<%# Eval("ProductUrl") %>' style="height: 83px; width: 83px" /></td></td>
</tr>
編輯代碼
protected void btnBuy_Click1(object sender, ImageClickEventArgs e)
{
// test to remind customer to login first
if ((string)Session["sFlag"] != "T")
{
Type csType = this.GetType();
ClientScript.RegisterStartupScript(csType, "Error", scriptErrorLogin);
}
else
{
OleDbConnection mDB = new OleDbConnection();
mDB.ConnectionString = "Provider = Microsoft.Jet.OLEDB.4.0;Data source="
+ Server.MapPath("~/App_Data/TabStoreDB.mdb");
OleDbCommand cmd;
//insert item purchased to itemsTable
int intOrderNo = (int)Session["sOrderNo"];
FormViewRow row0 = FormView_Product.Row;
String strProductId = ((Label)row0.FindControl("ProductID")).Text;
Int32 unitPrice = Convert.ToInt32(((Label)row0.FindControl("NormalPrice")).Text);
String strUnitPrice = ((Label)row0.FindControl("NormalPrice")).Text;
float floUnitPrice = float.Parse(strUnitPrice);
DropDownList DropDownList1 = (DropDownList)FormView_Product.FindControl("DropDownList1");
String strQty = DropDownList1.SelectedValue;
int intQty = int.Parse(strQty);
string strSQL = "INSERT INTO itemsTable(iOrderNo, iProductId, iQty, iUnitPrice)"
+ "VALUES (@OrderNo, @ProductId, @Qty, @UnitPrice)";
cmd = new OleDbCommand(strSQL, mDB);
cmd.Parameters.AddWithValue("@OrderNO", intOrderNo);
cmd.Parameters.AddWithValue("@ProductId", strProductId);
cmd.Parameters.AddWithValue("@Qty", intQty);
cmd.Parameters.AddWithValue("@UnitPrice", floUnitPrice);
mDB.Open();
cmd.ExecuteNonQuery();
mDB.Close();
Response.Redirect("ShoppingCart.aspx");
請勿使用主題行標記。標記線就是這樣。 – abatishchev