开发者

Add to cart in FormsView instead of DetailsView

Currently I have my product details page displayed in FormView which looks as follows:

Add to cart in FormsView instead of DetailsView

I have this chunk of codes for the add to cart button but it is for DetailsView. How do I modify the codes to work for 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 Codes:

</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>

Edited codes

 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");


Change these lines

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

To

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


Muhammad's code is the right direction to go, but the reason it won't work for you is you do not have any controls with the specified ID's in the FormViewRow. Try the following:

First, Add Label controls to your markup for ProductID and NormalPrice, named lblProductID and 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>

Now your code should work, but I'd change a couple of things (reason for the change in comments):

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;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜