Any button Onclick event is not working on my project. I inserted one more button called button1 for testing if it works but found that it is not working by changing the text of a label. Unfortunately, It is not working at all. Only page load event-driven is working.
The problem is not solved with other controls. For example, after the user clicks on an update button, a function 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.
Update:
This is the source code of the mentioned page (the web Form page is for Displaying and Updating shopping cart.One more control is there which is a checkout Button, to proceed with the next steps.
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="ShoppingCart.aspx.cs" Inherits="Celebreno.ShoppingCart" %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<div id="ShoppingCartTitle" runat="server" class="ContentHead">
<h1>Shopping Cart</h1>
</div>
<%----%>
<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>
<%----%>
<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>
<asp:Label ID="LblTotalText" runat="server" Text="Order Total: "></asp:Label>
<asp:Label ID="LblTotal" runat="server" EnableViewState="false"></asp:Label>
</div>
<br />
<table>
<tr>
<td>
<asp:Button ID="UpdateBtn" runat="server" Text="Update" OnClick="UpdateBtn_Click" CausesValidation="False" UseSubmitBehavior="false" />
</td>
<td>
<%--Checkout PlaceHolder--
<asp:ImageButton ID="CheckoutImageBtn" runat="server"
ImageUrl="https://www.paypal.com/en_US/i/btn/btn_xpressCheckout.gif"
Width="145" AlternateText="Check out with PayPal"
OnClick="CheckoutBtn_Click"
BackColor="Transparent" BorderWidth="0" CausesValidation="False" /> --%>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="CheckOut" CausesValidation="False" UseSubmitBehavior="false" />
</td>
</tr>
</table>
</asp:Content>
And this is the Code Behind of the web page:
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;
using System.Collections.Specialized;
using System.Collections;
using System.Web.ModelBinding;
namespace Celebreno
{
public partial class ShoppingCart : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (Actions_of_Cart usersShoppingCart = new Actions_of_Cart())
{
decimal cartTotal = 0;
cartTotal = usersShoppingCart.GetTotal();
if (cartTotal > 0)
{
LblTotal.Text = String.Format("{0:c}", cartTotal);
}
else
{
LblTotalText.Text = "";
LblTotal.Text = "";
ShoppingCartTitle.InnerText = "The Shopping Cart is Empty";
UpdateBtn.Visible = false;
}
}
}
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())
{
String cartId = usersShoppingCart.GetCartId();
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]);
cartUpdates[i].ProductId = Convert.ToInt32(rowValues["ProductID"]);
CheckBox cbRemove = new CheckBox();
cbRemove = (CheckBox)CartList.Rows[i].FindControl("Remove");
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();
LblTotal.Text = String.Format("{0:c}", usersShoppingCart.GetTotal());
return usersShoppingCart.GetCartItems();
}
}
public static IOrderedDictionary GetValues(GridViewRow row)
{
IOrderedDictionary values = new OrderedDictionary();
foreach (DataControlFieldCell cell in row.Cells)
{
if (cell.Visible)
{
cell.ContainingField.ExtractValuesFromCell(values, cell, row.RowState, true);
}
}
return values;
}
protected void UpdateBtn_Click(object sender, EventArgs e)
{
UpdateCartItems();
}
protected void CheckoutBtn(object sender, ImageClickEventArgs e)
{
using (Actions_of_Cart usersShoppingCart = new Actions_of_Cart())
{
Session["payment_amt"] = usersShoppingCart.GetTotal();
}
Response.Redirect("Checkout/CheckoutStart.aspx");
}
protected void Button1_Click(object sender, EventArgs e)
{
using (Actions_of_Cart usersShoppingCart = new Actions_of_Cart())
{
Session["payment_amt"] = usersShoppingCart.GetTotal();
}
Response.Redirect("Checkout/CheckoutStart.aspx");
}
}
}
What I have tried:
I have solved the problem of button event handling by adding these attributes. but when comes to other types of controls, these attributes cannot be included, so that problem is not solved.
<pre><asp:Button ID="UpdateBtn" runat="server" OnClick="UpdateBtn_Click" Text="Button" CausesValidation="False" UseSubmitBehavior="false" />