2011-07-31 20 views
-1

目前我已經顯示在FormView控件我的產品詳細信息頁面看起來如下:添加到購物車中FormsView而不是DetailsView控件

enter image description here

我對添加到購物車按鈕,這個塊代碼,但它是爲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">&nbsp;&nbsp; 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 /> 
     &nbsp;<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"); 
+0

請勿使用主題行標記。標記線就是這樣。 – abatishchev

回答

0

更改這些線

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 

FormViewRow row0 = FormView_Product.Row; 
String strProductId = ((Label)row0.FindControl("lblProductId")).Text; 
Int32 unitPrice =Convert.ToInt32(((Label)row0.FindControl("lblUnitPrice")).Text); 
+0

這假定他的DetailsView和他的FormView對於一行有相同的定義,我不確定這是否會如此。 – Tim

+0

是的,兩者都是一樣的。沒有區別。 –

+0

我的FormView ID是FormView_Product,所以我將它從DetailsView1.Rows [0]更改爲FormView_Product.Rows [0],但我收到一個錯誤「不包含行的定義」。我錯誤地定義了它嗎? – aurelio

0

穆罕默德的代碼是正確的方向去,但它不會爲你工作的原因是你沒有任何與FormViewRow中指定的ID的控件。請嘗試以下操作:

首先,添加標籤控件到您的標記的產品ID和NormalPrice,命名lblProductID和lblNormalPrice:

<td rowspan="7" valign="top"> 
    <br /> 
    <br /> 
    <asp:Label ID="lblProductID" Text='<%# Eval("ProductID") %>' runat="sever" /> 
    <span class="productname-text"><%# Eval("ProductBrand") %> <%# Eval("ProductModel") %></span> 
    <br /> 
    <span class="style1">Our Price:</span> 
    <asp:Label ID="lblNormalPrice" CssClass="price-text" Text='<%# Eval("S$" + "NromalPrice") %>' runat="server" /> 
    <br /> 
    <span class="description-text"><%# Eval("ProductDetails") %></span> 
</td> 

現在你的代碼應該工作,但我會改變一些事情(原因對於評論的變化):

FormViewRow row0 = FormView_Product.Row; 
string strProductId = ((Label)row0.FindControl("lblProductID")).Text; 
// Use TryParse in place of Parse; if the string is not parseable it will return false 
float unitPrice; 
float floUnitPrice = float.TryParse(((Lable)row0.FindControl("lblNormalPrice")).Text, out unitPrice) ? unitPrice : 0; 
// Use SelectedItem.Text instead of SelectedValue, as I didn't see any settings for Value in your markup, which might result in a blank string. 
int quantity; 
int intQty = int.TryParse(((DropDownList)row0.FindControl("DropDownList1")).SelectedItem.Text, out quantity) ? quantity : 0; 
相關問題