I am using .aspx in c# for creating food ordering system. Now my problem is in the menu part where I need to show toastr notications after I cliked this Image button.
<asp:ImageButton ID="btnAdd" runat="server" BorderStyle="None" Height="52px" ImageUrl="~/assets/img/Cart.png" Width="151px" CommandName="AddtoCart" />
Now, I’ve tried using toastr from NuGet packages
<link href="content/toastr.css" rel="stylesheet" />
<script src="Scripts/toastr.js"></script>
<script type="text/javascript">
$(function () {
$("#btnAdd").click(function () {
toastr["success"]("Items added To Cart")
toastr.options = {
"closeButton": true,
"debug": false,
"newestOnTop": false,
"progressBar": false,
"positionClass": "toast-top-right",
"preventDuplicates": false,
"onclick": null,
"showDuration": "300",
"hideDuration": "1000",
"timeOut": "5000",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
});
});
</script>
My problem is that alert(”) only works for this script not for the toastr, but the toastr works if I create a non runat=’server’ button,
like this one <button id="add">Add to Cart</button>
But doesn’t work in
<asp:Button ID="test" class="btn btn-primary" Text="Test" runat="server" />
and
<asp:ImageButton ID="btnAdd" runat="server" BorderStyle="None" Height="52px" ImageUrl="~/assets/img/Cart.png" Width="151px" CommandName="AddtoCart" />
whom is inside the
<form runat="server">
</form>
2
Answers
Pheww, just found a solution for my question, here it is. You can put this code in the master page content head or aspx content head:
Plus put this code in the aspx.cs like this one
Also this is also compatible with datalistitems with buttons or image button like this one:
Yow thanks for the help guys
I am adding this to make understand what could be one of the reason.
You might be using
Master Pages
orUser Control
which will change the ID#btnAdd
which you are referring the javascript. Solution could be use theclientIdMode = static
it will not change the ID of the control. But look for duplicate Id issue.Normally server controls have a server methods which cause postback in this case you client script will not work. So you need to consider that what do you want to do with that. OnClientClick is the method which I remember used for client side funcaiton.