【问题标题】:event handling is not working in my asp.net web forms web application事件处理在我的 asp.net Web 表单 Web 应用程序中不起作用
【发布时间】:2019-11-22 13:37:59
【问题描述】:

更新按钮Onclick 事件不起作用。

我又插入了一个名为 button1 的按钮来测试它是否有效,但发现它不起作用,甚至在应用程序的其他 Web 表单中被 Icacked 了

它根本不工作。

只有页面加载事件驱动有效。

更新: 使用其他控件无法解决此问题。例如,在用户点击更新按钮后,将调用一个函数来检查文本框中的值并进行一些计算。 调试后发现程序无法从文本框中读取任何值。

注意:我正在使用诸如 bootstrap、Ajax 之类的包,因为它们已经内置在 asp.net Web 表单 Web 应用程序中。正如一些参考资料中提到的,我还尝试将它们排除在项目之外,但是没有任何改变。

这是我的购物车网络表单代码:

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

源代码:

<%@ 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>

【问题讨论】:

  • 你认为Button1.Click += Button1_Click; 在做什么?在我看来,问题可能出在哪里。
  • 这是我尝试解决问题的方法之一,但它不起作用。我忘记删除了。

标签: c# html asp.net asp.net-mvc


【解决方案1】:

已解决:通过添加如下所示的两个属性

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

【讨论】:

    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 2021-05-18
    • 1970-01-01
    • 1970-01-01
    • 2011-07-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多