简体   繁体   中英

asp.net snackbar not working on button click c#

On asp.net button click, i want to call this function to show the snackbar. However, it failed to do so. I tried putting the button outside the form runat="server" and it works. However, I need it to be inside the form runat="server" tag and on click of asp.net button, how do I solve this issue?

Home.aspx

<style>
    #snackbar {
        visibility: hidden;
        min-width: 250px;
        margin-left: -125px;
        background-color: #333;
        color: #fff;
        text-align: center;
        border-radius: 2px;
        padding: 16px;
        position: fixed;
        z-index: 1;
        left: 50%;
        bottom: 30px;
        font-size: 17px;
    }

        #snackbar.show {
            visibility: visible;
            -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
            animation: fadein 0.5s, fadeout 0.5s 2.5s;
        }

    @-webkit-keyframes fadein {
        from {
            bottom: 0;
            opacity: 0;
        }

        to {
            bottom: 30px;
            opacity: 1;
        }
    }

    @keyframes fadein {
        from {
            bottom: 0;
            opacity: 0;
        }

        to {
            bottom: 30px;
            opacity: 1;
        }
    }

    @-webkit-keyframes fadeout {
        from {
            bottom: 30px;
            opacity: 1;
        }

        to {
            bottom: 0;
            opacity: 0;
        }
    }

    @keyframes fadeout {
        from {
            bottom: 30px;
            opacity: 1;
        }

        to {
            bottom: 0;
            opacity: 0;
        }
    }
</style>

<script>
    function myFunction() {
        var x = document.getElementById("snackbar");
        x.className = "show";
        setTimeout(function () { x.className = x.className.replace("show", ""); }, 3000);
    }
</script>

<form runat="server">
    <asp:LinkButton ID="reportThreadBtn" class="btn btn-default btn-sm" ForeColor="red" runat="server" OnClick="myFunction()" Font-Size="Medium"><i class="glyphicon glyphicon-flag"></i></asp:LinkButton>
</form>

To achieve this you can use OnClientClick .

Make sure that the javascript that you put in it returns a value of false , this ensures that the default LinkButton behaviour (which is: activating the Form PostBack) is not executed.

<asp:LinkButton .... OnClientClick="myFunction(); return false;" ...> ... </asp:LinkButton>

Update: below is the full code of my /Default.aspx , and with this code it works - the black Snackbar box pops up at the bottom (although it flashes a little when it disappears, I did not investigate why that is).
I created it as part of a brand new, as simple as possible and otherwise empty "ASP.NET Web Application" (.NET 4.7.2). Check for differences between my code and yours, especially HTML/CSS, and make changes as needed.

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebForms1._Default" %>

<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">

    <style>
        #snackbar {
            visibility: hidden;
            min-width: 250px;
            margin-left: -125px;
            background-color: #333;
            color: #fff;
            text-align: center;
            border-radius: 2px;
            padding: 16px;
            position: fixed;
            z-index: 1;
            left: 50%;
            bottom: 30px;
            font-size: 17px;
        }

        #snackbar.show {
            visibility: visible;
            -webkit-animation: fadein 0.5s, fadeout 0.5s 2.5s;
            animation: fadein 0.5s, fadeout 0.5s 2.5s;
        }

        @-webkit-keyframes fadein {
            from {
                bottom: 0;
                opacity: 0;
            }

            to {
                bottom: 30px;
                opacity: 1;
            }
        }

        @keyframes fadein {
            from {
                bottom: 0;
                opacity: 0;
            }

            to {
                bottom: 30px;
                opacity: 1;
            }
        }

        @-webkit-keyframes fadeout {
            from {
                bottom: 30px;
                opacity: 1;
            }

            to {
                bottom: 0;
                opacity: 0;
            }
        }

        @keyframes fadeout {
            from {
                bottom: 30px;
                opacity: 1;
            }

            to {
                bottom: 0;
                opacity: 0;
            }
        }
    </style>

    <script>
        function myFunction() {
            var x = document.getElementById("snackbar");
            x.className = "show";
            setTimeout(function () { x.className = x.className.replace("show", ""); }, 3000);
        }
    </script>

    <br/>
    <asp:LinkButton ID="reportThreadBtn" class="btn btn-default btn-sm" ForeColor="red" runat="server" OnClientClick="myFunction(); return false;" Font-Size="Medium">CLICK IT</asp:LinkButton>
    <br/>

    <div id="snackbar"><h2>snackbar</h2></div>
</asp:Content>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM