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.


<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">
      <asp:BoundField DataField="Name" HeaderText="Product Name" ReadOnly="True" SortExpression="Name" >
        <ControlStyle Width="100%" />
      <asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" ReadOnly="True"
        SortExpression="Price" />
    <asp:TemplateField HeaderText="Color">
            <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>
      <asp:TemplateField HeaderText="Quantity">
          <asp:TextBox ID="editQuantity" runat="server" CssClass="GridEditingRow" Width="24px" MaxLength="2" Text='<%#Eval("Quantity")%>' />
      <asp:BoundField DataField="Subtotal" DataFormatString="{0:c}" HeaderText="Subtotal"
        ReadOnly="True" SortExpression="Subtotal" />
      <asp:ButtonField ButtonType="Button" CommandName="Delete" Text="Delete" >
        <ControlStyle CssClass="SmallButtonText " />
  <table width="100%">
        <span class="ProductDescription">
          Total amount:
        <asp:Label ID="totalAmountLabel" runat="server" Text="Label" CssClass="ProductPrice" />
      <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" />
    <br />
  <uc1:ProductRecommendations ID="recommendations" runat="server" />


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)

        // fill the shopping cart controls with data
    private void PopulateControls()
            // display product recommendations

            // 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);
            // if the shopping cart is not empty...
                // populate the list with the shopping cart content
            grid.DataSource = dt;
                // 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

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

        // redirect to login page
    protected void loginButton_Click(object sender, EventArgs e)

        // redirect to register page
    protected void registerButton_Click(object sender, EventArgs e)

1. From product page, you can pass the selected value of the dropdown list through session or query string.
2. In shopping cart page, in the RowDataBound event of GridView, find the dropdownlist control from the GridView row and set selected value to the value received from session or query string.