Disable asp.net button postback

My idea is that when the user is not registered, he will not be able to add the product to the basket, so I did it like this:

<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 the 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, using Bootstrap, I pressed a button that it cannot click, but in fact it is still clickable.

So, I thought that mabey, if I disable the postback button, it really will not be clickable.

How to disable button postback?

I 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.

+4
source share
2 answers

, , (, ), , :

  • BootStrap
  • false OnClientClick

:

if (Session["User"] == null)
{
    BTNAddToCart.CssClass = "btn btn-info btn-lg disabled";
    BTNAddToCart.OnClientClick = "return false;";
    BTNAddToCart.Attributes.Add("onfocus", "this.blur();");
    ...
}
+1

asp.net false, .

BTNAddToCart.Enabled = false;

disabled, disabled css, . :

input:disabled {
    background: #444;
}

: css

+3

All Articles