Hi everyone, I've got a DropDownList in Product.aspx when the add to cart button is pressed the value selected is passed onto a stored procedure. What I need help with is in ShoppingCart.aspx I want the selected value on the previous page to be the index on this pages DropDownList. But the DropDownList is in a GridView and therefore I do not know how to do this.
Any help will be appreciated.
ShoppingCart.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="titleLabel" runat="server" Text="Your Shopping Cart" CssClass="ShoppingCartTitle" />
<br />
<asp:Label ID="statusLabel" CssClass="AdminPageText" ForeColor="Red" runat="server" /><br />
<asp:GridView ID="grid" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID" Width="100%" BorderWidth="0px" OnRowDeleting="grid_RowDeleting">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Product Name" ReadOnly="True" SortExpression="Name" >
<ControlStyle Width="100%" />
</asp:BoundField>
<asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" ReadOnly="True"
SortExpression="Price" />
<asp:TemplateField HeaderText="Color">
<ItemTemplate>
<asp:DropDownList ID="editColor" runat="server" DataSourceID="SqlDataSource1" DataTextField="Value" DataValueField="Value" /><asp:SqlDataSource
ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:BalloonShopConnectionString %>"
SelectCommand="SELECT [Value] FROM [AttributeValue]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Quantity">
<ItemTemplate>
<asp:TextBox ID="editQuantity" runat="server" CssClass="GridEditingRow" Width="24px" MaxLength="2" Text='<%#Eval("Quantity")%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Subtotal" DataFormatString="{0:c}" HeaderText="Subtotal"
ReadOnly="True" SortExpression="Subtotal" />
<asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete" >
<ControlStyle CssClass="SmallButtonText " />
</asp:ButtonField>
</Columns>
</asp:GridView>
<table width="100%">
<tr>
<td>
<span class="ProductDescription">
Total amount:
</span>
<asp:Label ID="totalAmountLabel" runat="server" Text="Label" CssClass="ProductPrice" />
</td>
<td align="right">
<asp:Button ID="updateButton" runat="server" Text="Update" CssClass="SmallButtonText" OnClick="updateButton_Click" />
<asp:Button ID="loginButton" runat="server" CssClass="SmallButtonText" Text="Login" Width="80px" OnClick="loginButton_Click" />
<asp:Button ID="registerButton" runat="server" CssClass="SmallButtonText" Text="Register" Width="80px" OnClick="registerButton_Click" />
</td>
</tr>
</table>
<br />
<uc1:ProductRecommendations ID="recommendations" runat="server" />
</asp:Content>
public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// popluate the control only in the initial page load
if (!IsPostBack)
PopulateControls();
}
// fill the shopping cart controls with data
private void PopulateControls()
{
// display product recommendations
recommendations.LoadCartRecommendations();
// get the items in the shopping cart
DataTable dt = ShoppingCartAccess.GetItems();
// if the shopping cart is empty...
if (dt.Rows.Count == 0)
{
titleLabel.Text = "Your Shopping Cart is empty!";
grid.Visible = false;
updateButton.Enabled = false;
//checkoutButton.Enabled = false;
totalAmountLabel.Text = String.Format("{0:c}", 0);
}
else
// if the shopping cart is not empty...
{
// populate the list with the shopping cart content
grid.DataSource = dt;
grid.DataBind();
// set up controls
titleLabel.Text = "These are the products in your Shopping Cart:";
grid.Visible = true;
updateButton.Enabled = true;
//checkoutButton.Enabled = true;
// display the total amount
decimal amount = ShoppingCartAccess.GetTotalAmount();
totalAmountLabel.Text = String.Format("{0:c}", amount);
}
}
// remove a product from the cart
protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
// index of the row beeing deleted
int rowIndex = e.RowIndex;
// the ID of the product being deleted
string productId = grid.DataKeys[rowIndex].Value.ToString();
// remove the product from the shopping cart
bool success = ShoppingCartAccess.RemoveItem(productId);
// display the status
statusLabel.Text = success ? "Product successfuly removed! " :
"There was an error removing the product! ";
// repopulate the control
PopulateControls();
}
// updat shopping cart product quantities
protected void updateButton_Click(object sender, EventArgs e)
{
// number of rows in the grid view
int rowsCount = grid.Rows.Count;
// will store a row of the gridview
GridViewRow gridRow;
// will reference a quantity textBox in the GridView
TextBox quantityTextBox;
DropDownList editColor;
// variable to store productId and quantity
string productId;
int quantity;
// was the update successful?
bool success = true;
// go through the rows of the gridview
for (int i = 0; i < rowsCount; i++)
{
// get a row
gridRow = grid.Rows[i];
// the Id of the product being updated
productId = grid.DataKeys[i].Value.ToString();
// get the quantity textbox in the row
quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
editColor = (DropDownList)gridRow.FindControl("editColor");
string attributes = editColor.SelectedValue;
// get the quantity, gaurding against bogus values
if (Int32.TryParse(quantityTextBox.Text, out quantity))
{
// update the product quantity
success = success && ShoppingCartAccess.UpdateItem(productId, quantity, attributes);
}
else
{
// if TryParse did not succeed
success = false;
}
// display the status message
statusLabel.Text = success ?
"Your Shopping Cart was successfuly updated!" :
"Some quantity updates failed! Please verify your cart!";
}
// repopulate the control
PopulateControls();
}
// redirect to login page
protected void loginButton_Click(object sender, EventArgs e)
{
Response.Redirect("Checkout.aspx");
}
// redirect to register page
protected void registerButton_Click(object sender, EventArgs e)
{
Response.Redirect("Register.aspx");
}
}