Reputation: 183
My idea is when the user isn't loged in he won't be able to add an item to the shopping cart, so what I did is like so:
<asp:Button ID="BTNAddToCart" runat="server" Text="Add to cart" class="btn btn-info btn-lg" style="display: inline; margin: auto; display: block; visibility: hidden;" OnClick="BTNAddToCart_Click" />
And then on code behind:
if (Session["User"] == null)
{
BTNAddToCart.Attributes["class"] = "btn btn-info btn-lg disabled";
BTNAddToCart.Attributes.Add("title", "Error");
BTNAddToCart.Attributes.Add("data-toggle", "popover");
BTNAddToCart.Attributes.Add("data-trigger", "hover");
BTNAddToCart.Attributes.Add("data-placement", "bottom");
BTNAddToCart.Attributes.Add("data-content", "You must be loged in to add items to the cart");
}
As You can see, by using Bootstrap I gave the button the look which he can't be clicked, but in reality He is still clickable.
So I thought that mabey if I will disable the button postback it will really be not clickable.
How can I disable the button postback?
I have tried:
<asp:Button ID="BTNAddToCart" runat="server" Text="Add to cart" class="btn btn-info btn-lg" style="display: inline; margin: auto; display: block; visibility: hidden;" OnClientClick="BTNJavaScriptClick()" OnClick="BTNAddToCart_Click" />
<script>
function BTNJavaScriptClick()
{
var ButtonAdd = document.getElementById("BTNAddToCart");
if (ButtonAdd.className == "btn btn-info btn-lg disabled")
return false;
}
</script>
I even tried BTNAddToCart.Enabled = false;
and it worked but it made my popover disapper.
Upvotes: 4
Views: 1711
Reputation: 73761
If you want the button to react when the mouse is over it (e.g. show a tooltip) while not triggering a postback when clicked, you can:
false
in OnClientClick
The code would look like this:
if (Session["User"] == null)
{
BTNAddToCart.CssClass = "btn btn-info btn-lg disabled";
BTNAddToCart.OnClientClick = "return false;";
BTNAddToCart.Attributes.Add("onfocus", "this.blur();");
...
}
Upvotes: 1
Reputation: 66649
You can simple set Enable asp.net attribute to false and the button will not post back.
BTNAddToCart.Enabled = false;
When an element is disabled
you can use the disabled
style on css to give him the desired look. Here is a simple example:
input:disabled {
background: #444;
}
relative to that: Styling a disabled input with css only
Upvotes: 3