简体   繁体   中英

event handling is not working in my asp.net web forms web application

The update button Onclick event is not working.

I inserted one more button called button1 for testing if it works, but found that it is not working, even Icacked in other web forms in the app

It is not working at all.

Only page load event-driven is working.

Update: The problem is not solved with other controls. For example, after the user clicks on an update button, a fucntion will be called that checks the value in the textbox and make some calculation. After debugging, I found that the program cannot read any values from the textbox.

Note: I am using packages like bootstrap, Ajax as they are already built-in in the asp.net web forms web applications. I have also tried to exclude them from the project as mentioned in some references, however, nothing changed.

Here is my code for Shopping Cart web form:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Celebreno.Models;
using Celebreno.Cart;

//to use IOrderedDictionary
using System.Collections.Specialized;

using System.Collections;
using System.Web.ModelBinding;



namespace Celebreno
{
    public partial class ShowCart : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //2- Display the total of cart by calling GetTotal() function(located in Actions_of_Cart class)
            //and then store the return value in a new variable, then dispaly it in a label
            using (Actions_of_Cart usersShoppingCart = new Actions_of_Cart())
            {

            //declare a new variable to store the total into it
            decimal cartTotal = 0;
            cartTotal = usersShoppingCart.GetTotal();

            if (cartTotal > 0)
            {
                    //Display Total in a label that has property " Enable ViewState = False"
                    //      ViewSate used
                    LblTotal.Text = String.Format("{0:c}", cartTotal);
            }
            else
            {
                LblTotalText.Text = "";
                LblTotal.Text = "";
                    //HtmlGenericControl
                    ShoppingCartTitle.InnerText = "The Shopping Cart is Empty";
                    //not yet needed
                    UpdateBtn.Visible = false;
             //   CheckoutImageBtn.Visible = false;
            }


                Button1.Click += Button1_Click;
            }

    }


        //1- decalre the "select Method" (used in GridView in Source Code) to return the value of..
        //..calling GetCartItems(), which is defined in Actions_of_Cart DB class
        public List<ItemsInCart> GetShoppingCartItems()
        {
            Actions_of_Cart actions = new Actions_of_Cart();
            return actions.GetCartItems();
        }



        //
        public List<ItemsInCart> UpdateCartItems()
        {
            using (Actions_of_Cart usersShoppingCart = new Actions_of_Cart())
            {

                //new var
                String cartId = usersShoppingCart.GetCartId();

                //The UpdateCartItems method gets the updated values for each item in the shopping cart.
                //Then, the UpdateCartItems method calls the UpdateShoppingCartDatabase method
                //to either add or remove items from the shopping cart.
                //Once the database has been updated to reflect the updates to the shopping cart,
                //the GridView control is updated on the shopping cart page by calling the DataBind method
                //for the GridView. Also, the total order amount on the shopping cart page is updated
                //to reflect the updated list of items.
                Actions_of_Cart.ShoppingCartUpdates[] cartUpdates = new Actions_of_Cart.ShoppingCartUpdates[CartList.Rows.Count];
                for (int i = 0; i < CartList.Rows.Count; i++)
                {
                    IOrderedDictionary rowValues = new OrderedDictionary();
                    rowValues = GetValues(CartList.Rows[i]);
                    //ProductId related to struct in Actions
                    cartUpdates[i].ProductId = Convert.ToInt32(rowValues["ID"]);

                    CheckBox cbRemove = new CheckBox();
                    cbRemove = (CheckBox)CartList.Rows[i].FindControl("RemoveItem");
                    cartUpdates[i].RemoveItem = cbRemove.Checked;

                    TextBox quantityTextBox = new TextBox();
                    quantityTextBox = (TextBox)CartList.Rows[i].FindControl("PurchaseQuantity");
                    cartUpdates[i].PurchaseQuantity = Convert.ToInt16(quantityTextBox.Text.ToString());
                }
                usersShoppingCart.UpdateShoppingCartDatabase(cartId, cartUpdates);

                CartList.DataBind();

                //dispaly updated total value
                LblTotal.Text = String.Format("{0:c}", usersShoppingCart.GetTotal());

                return usersShoppingCart.GetCartItems();
            }
        }



        public static IOrderedDictionary GetValues(GridViewRow row)
        {
            try
            {
                IOrderedDictionary values = new OrderedDictionary();
                foreach (DataControlFieldCell cell in row.Cells)
                {
                    if (cell.Visible)
                    {
                        // Extract values from the cell.
                        cell.ContainingField.ExtractValuesFromCell(values, cell, row.RowState, true);
                    }
                }
                return values;
            }
            catch (Exception exp)
            {
                throw new Exception("ERROR1" + exp.Message.ToString(), exp);
            }
        }

        protected void UpdateBtn_Click(object sender, EventArgs e)
        {
            UpdateCartItems();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text = "IT is working ";
        }
    }
}

Source Code:

 <%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ShowCart.aspx.cs" Inherits="Celebreno.ShowCart" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server"> <div id="ShoppingCartTitle" runat="server" class="ContentHead"><h1>Shopping Cart</h1></div> <%--Start of the GridView to display the items in the cart--%> <asp:GridView ID="CartList" runat="server" AutoGenerateColumns="False" ShowFooter="True" GridLines="Vertical" CellPadding="4" ItemType="Celebreno.Models.ItemsInCart" SelectMethod="GetShoppingCartItems" CssClass="table table-striped table-bordered" > <Columns> <%--problem solved: change "ID" to "ServicePack.ID"--%> <asp:BoundField DataField="ServicePack.ID" HeaderText="Service Package ID" SortExpression="ID" /> <asp:BoundField DataField="ServicePack.Provider" HeaderText="Provider" /> <asp:BoundField DataField="ServicePack.UnitPrice" HeaderText="Price (each)" DataFormatString="{0:c}"/> <asp:TemplateField HeaderText="Quantity"> <ItemTemplate> <asp:TextBox ID="PurchaseQuantity" Width="40" runat="server" Text="<%#: Item.Quantity %>"></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Item Total"> <ItemTemplate> <%#: String.Format("{0:c}", ((Convert.ToDouble(Item.Quantity)) * Convert.ToDouble(Item.ServicePack.UnitPrice)))%> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Remove Item"> <ItemTemplate> <%--checkbox for removing the item--%> <asp:CheckBox id="Remove" runat="server"></asp:CheckBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <%--End of the GridView--%> <div> <p></p> <strong> <%--2 (display the total of the cart--%> <asp:Label ID="LblTotalText" runat="server" Text="Order Total: "></asp:Label> <asp:Label ID="LblTotal" runat="server" EnableViewState="false"></asp:Label> </strong> </div> <br /> <table> <tr> <td> <%--3 ( add update button) --%> </td> <td> <asp:Button ID="UpdateBtn" runat="server" Text="Button" OnClick="UpdateBtn_Click" /> <asp:Button ID="Button1" runat="server" Text="Test" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="Test"></asp:Label> </td> </tr> </table> </asp:Content>

solved: by adding two attributes as shown below

<asp:Button ID="UpdateBtn" runat="server" OnClick="UpdateBtn_Click" Text="Button" CausesValidation="False" UseSubmitBehavior="false" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM