示例#1
0
    protected void Update_Click(object sender, EventArgs e)
    {
        // update shopping cart product quantities
// Number of rows in the GridView
        int rowsCount = grid.Rows.Count;
// Will store a row of the GridView
        GridViewRow gridRow;
// Will reference a quantity TextBox in the GridView
        TextBox quantityTextBox;
// Variables to store product ID 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 deleted
            productId = grid.DataKeys[i].Value.ToString();
// Get the quantity TextBox in the Row

            quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
// Get the quantity, guarding against bogus values
            if (Int32.TryParse(quantityTextBox.Text, out quantity))
            {
// Update product quantity
                success = success && ShoppingCartAccess.UpdateItem(productId, quantity);
            }
            else
            {
// if TryParse didn't succeed
                success = false;
            }
// Display status message
            statusLabel.Text = success ?
                               "Your shopping cart was successfully updated!" :
                               "Some quantity updates failed! Please verify your cart!";
        }
// Repopulate the control
        PopulateControls();
    }
    // update shopping cart product quantities
    protected void updateButton_Click(object sender, EventArgs e)
    {
        // Number of rows in the GridView
        int rowsCount = grid.Rows.Count;
        // Will store a row of the GridView
        GridViewRow gridRow;
        // Will reference a quantity TextBox in the GridView
        TextBox quantityTextBox;
        // Variables to store product ID 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 deleted
            productId = grid.DataKeys[i].Value.ToString();
            // Get the quantity TextBox in the Row
            quantityTextBox = (TextBox)gridRow.FindControl("editQuantity");
            // Get the quantity, guarding against bogus values
            if (Int32.TryParse(quantityTextBox.Text, out quantity))
            {
                // Update product quantity
                success = success && ShoppingCartAccess.UpdateItem(productId, quantity);
            }
            else
            {
                // if TryParse didn't succeed
                success = false;
            }

            // Display status message
            statusLabel.Text = success ?
                               "Кошницата Ви беше променена успешно!" :
                               "Някой промени по количеството се провалиха! Моля проверете кошницата си!";
        }
        // Repopulate the control
        PopulateControls();
    }