How to make the add to cart button to cannot click it when the quantity is 0, means no product stock in the system.
Product.aspx
<%@ Page Title="" Language="C#" MasterPageFile="~/User.master" AutoEventWireup="true" CodeFile="ProductView.aspx.cs" Inherits="ProductView" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<br />
<br />
<div class="container">
<div style="padding-top: 50px">
<div id="divSuccess" runat="server" class="alert alert-success alert-dismissible fade in h4">
<a href="#" class="close" data-dismiss="alert" aria-label="close">×</a>
Berjaya! Produk berjaya dimasukkan ke dalam Cart. <a href="Cart.aspx">Lihat Cart</a>
</div>
</div>
<div style="padding-top:50px">
<div class="col-md-5">
<div style="max-width:480px" class="thumbnail">
<%----%>
<div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
<li data-target="#carousel-example-generic" data-slide-to="1"></li>
<li data-target="#carousel-example-generic" data-slide-to="2"></li>
<li data-target="#carousel-example-generic" data-slide-to="3"></li>
<li data-target="#carousel-example-generic" data-slide-to="4"></li>
</ol>
<div class="carousel-inner" role="listbox">
<asp:repeater ID="rptrImage" runat="server">
<ItemTemplate>
<div class="item <%# GetActiveImgClass(Container.ItemIndex) %>">
<img src="Images/ProductImages/<%# Eval("PID") %>/<%# Eval("Name") %><%# Eval("Extention") %>" alt="<%# Eval("Name") %>" onerror="this.src='Images/ImageNotAvailable.jpg'">
</div>
</ItemTemplate>
</asp:repeater>
</div>
<a class="left carousel-control" href="#carousel-example-generic" role="button" data-slide="prev">
Previous
</a>
<a class="right carousel-control" href="#carousel-example-generic" role="button" data-slide="next">
Next
</a>
</div>
<%----%>
</div>
</div>
<div class="col-md-5">
<asp:Repeater ID="rptrProductDetails" runat="server" OnItemDataBound="rptrProductDetails_ItemDataBound" >
<ItemTemplate>
<div class="divDet1">
<h1 class="proNameView"><%# Eval("PName") %> </h1>
<%#Eval("PPrice","{0:c}") %> Diskaun RM
<%# string.Format("{0}",Convert.ToInt64(Eval("PPrice"))-Convert.ToInt64(Eval("PSelPrice"))) %><p class="proPriceView"> <%#Eval("PSelPrice","{0:c}") %></p>
</div>
<div >
<h5 class="h5size"></h5>
<div>
<asp:radiobuttonlist ID="rblSize" runat="server" RepeatDirection="Horizontal" >
<asp:ListItem Value="S" Text="S"></asp:ListItem>
<asp:ListItem Value="M" Text="M"></asp:ListItem>
<asp:ListItem Value="L" Text="L"></asp:ListItem>
<asp:ListItem Value="XL" Text="XL"></asp:ListItem>
</asp:radiobuttonlist>
</div>
</div>
<div class="divDet1">
<asp:button ID="btnAddtoCart" CssClass="btnQuickCart" runat="server" text="ADD TO CART" OnClick="btnAddtoCart_Click"/>
<asp:Label ID="lblError" CssClass ="text-danger " runat="server" ></asp:Label>
</div>
<div class="divDet1">
<h5 class="h5size"> Kuantiti Produk</h5>
<p> <%#Eval("PQuantity") %> </p>
<h5 class="h5size"> Penerangan Produk</h5>
<p> <%#Eval("PDescription") %> </p>
<h5 class="h5size">Butiran Produk</h5>
<p> <%#Eval("PProductDetails") %> </p>
<h5 class="h5size"> Tarikh Luput Produk</h5>
<p> <%#Eval("PMaterialCare") %></p>
</div>
<div >
<p><%# ((int)Eval("FreeDelivery")==1)? "Ambil di kedai":"" %> </p>
<p><%# ((int)Eval("30DayRet")==1)? "30 Days Returns":"" %></p>
<p><%# ((int)Eval("COD")==1)? "Cash on Delivery":"" %></p>
</div>
<asp:HiddenField ID="hfCatID" runat="server" Value='<%# Eval("PcategoryID") %>' />
<asp:HiddenField ID="hfSubCatID" runat="server" Value='<%# Eval("PSubCatID") %>' />
<asp:HiddenField ID="hfGenderID" runat="server" Value='<%# Eval("PGender") %>' />
<asp:HiddenField ID="hfBrandID" runat="server" Value='<%# Eval("PBrandID") %>' />
</ItemTemplate>
</asp:Repeater>
</div>
</div>
</div>
</asp:Content>
Product.aspx.cs
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class ProductView : System.Web.UI.Page
{
readonly Int32 myQty = 1;
public static String CS = ConfigurationManager.ConnectionStrings["db"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["PID"] != null)
{
if (!IsPostBack)
{
divSuccess.Visible = false;
BindProductDetails();
BindProductImage();
BindCartNumber();
}
}
else
{
Response.Redirect("~/Products.aspx");
}
}
public void BindCartNumber()
{
if (Session["USERID"] != null)
{
string UserIDD = Session["USERID"].ToString();
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("BindCartNumberz", con)
{
CommandType = CommandType.StoredProcedure
};
cmd.Parameters.AddWithValue("@UserID", UserIDD);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
string CartQuantity = dt.Compute("Sum(Qty)", "").ToString();
}
else
{
}
}
}
}
}
private void BindProductDetails()
{
Int64 PID = Convert.ToInt64(Request.QueryString["PID"]);
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("BindProductDetails", con)
{
CommandType = CommandType.StoredProcedure
};
cmd.Parameters.AddWithValue("@PID", PID);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptrProductDetails.DataSource = dt;
rptrProductDetails.DataBind();
Session["CartPID"] = Convert.ToInt32(dt.Rows[0]["PID"].ToString());
Session["myPName"] = dt.Rows[0]["PName"].ToString();
Session["myPPrice"] = dt.Rows[0]["PPrice"].ToString();
Session["myPSelPrice"] = dt.Rows[0]["PSelPrice"].ToString();
}
}
}
private void BindProductImage()
{
Int64 PID = Convert.ToInt64(Request.QueryString["PID"]);
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("BindProductImages", con)
{
CommandType = CommandType.StoredProcedure
};
cmd.Parameters.AddWithValue("@PID", PID);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rptrImage.DataSource = dt;
rptrImage.DataBind();
}
}
}
protected string GetActiveImgClass(int ItemIndex)
{
if (ItemIndex == 0)
{
return "active";
}
else
{
return "";
}
}
protected void rptrProductDetails_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string BrandID = (e.Item.FindControl("hfBrandID") as HiddenField).Value;
string CatID = (e.Item.FindControl("hfCatID") as HiddenField).Value;
string SubCatID = (e.Item.FindControl("hfSubCatID") as HiddenField).Value;
string GenderID = (e.Item.FindControl("hfGenderID") as HiddenField).Value;
RadioButtonList rblSize = e.Item.FindControl("rblSize") as RadioButtonList;
using (SqlConnection con = new SqlConnection(CS))
{
using (SqlCommand cmd = new SqlCommand("select * from tblSizes where BrandID='" + BrandID + "' and CategoryID=" + CatID + " and SubCategoryID=" + SubCatID + " and GenderID=" + GenderID + "", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
rblSize.DataSource = dt;
rblSize.DataTextField = "sizename";
rblSize.DataValueField = "sizeid";
rblSize.DataBind();
}
}
}
}
}
protected void btnAddtoCart_Click(object sender, EventArgs e)
{
if (Session["Username"] != null)
{
Int32 UserID = Convert.ToInt32(Session["USERID"].ToString());
Int64 PID = Convert.ToInt64(Request.QueryString["PID"]);
using (SqlConnection con = new SqlConnection(CS))
{
con.Open();
SqlCommand cmd = new SqlCommand("IsProductExistInCart", con)
{
CommandType = CommandType.StoredProcedure
};
cmd.Parameters.AddWithValue("@UserID", UserID);
cmd.Parameters.AddWithValue("@PID", PID);
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
Int32 updateQty = Convert.ToInt32(dt.Rows[0]["Qty"].ToString());
SqlCommand myCmd = new SqlCommand("UpdateCart", con)
{
CommandType = CommandType.StoredProcedure
};
myCmd.Parameters.AddWithValue("@Quantity", updateQty + 1);
myCmd.Parameters.AddWithValue("@CartPID", PID);
myCmd.Parameters.AddWithValue("@UserID", UserID);
Int64 CartID = Convert.ToInt64(myCmd.ExecuteScalar());
BindCartNumber();
divSuccess.Visible = true;
}
else
{
SqlCommand myCmd = new SqlCommand("InsertCart", con)
{
CommandType = CommandType.StoredProcedure
};
myCmd.Parameters.AddWithValue("@UID", UserID);
myCmd.Parameters.AddWithValue("@PID", Session["CartPID"].ToString());
myCmd.Parameters.AddWithValue("@PName", Session["myPName"].ToString());
myCmd.Parameters.AddWithValue("@PPrice", Session["myPPrice"].ToString());
myCmd.Parameters.AddWithValue("@PSelPrice", Session["myPSelPrice"].ToString());
myCmd.Parameters.AddWithValue("@Qty", myQty);
Int64 CartID = Convert.ToInt64(myCmd.ExecuteScalar());
con.Close();
BindCartNumber();
divSuccess.Visible = true;
}
}
}
}
else
{
Int64 PID = Convert.ToInt64(Request.QueryString["PID"]);
Response.Redirect("~/Signin.aspx?rurl=" + PID);
}
}
protected void btnCart2_ServerClick(object sender, EventArgs e)
{
Response.Redirect("~/Cart.aspx");
}
protected override void InitializeCulture()
{
CultureInfo ci = new CultureInfo("ms-MY");
ci.NumberFormat.CurrencySymbol = "RM";
Thread.CurrentThread.CurrentCulture = ci;
base.InitializeCulture();
}
}
What I have tried:
Where to put the condition quantity=0 in this code? I don't have any idea.
I want to make either the button turn to grey colour (cannot click) or pop up message.