【问题标题】:asp.net snackbar not working on button click c#asp.net 小吃店在按钮单击 c# 上不起作用
【发布时间】:2021-01-08 13:09:29
【问题描述】:

单击 asp.net 按钮时,我想调用此函数来显示小吃栏。然而,它未能如愿。 我尝试将按钮放在表单 runat="server" 之外,它可以工作。 但是,我需要它位于表单 runat="server" 标记内并单击 asp.net 按钮,我该如何解决这个问题?

主页.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>

【问题讨论】:

    标签: c# asp.net


    【解决方案1】:

    为此,您可以使用OnClientClick

    确保您放入其中的 javascript 返回值 false,这样可以确保不执行默认的 LinkBut​​ton 行为(即:激活 Form PostBack)。 p>

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

    更新:下面是我的 /Default.aspx 的完整代码,使用此代码可以正常工作 - 黑色的 Snackbar 框在底部弹出(虽然它消失时会闪烁一点,我没有调查为什么会这样)。
    我将它创建为一个全新的、尽可能简单且为空的“ASP.NET Web 应用程序”(.NET 4.7.2)的一部分。检查我的代码和你的代码之间的差异,尤其是 HTML/CSS,并根据需要进行更改。

    <%@ 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>
    

    【讨论】:

    • 我也试过了,也没用:(
    • 我更新了答案,请检查。我试过了,它对我有用。
    • 我试过了,但它也不起作用! 点我​​ton>
    • 我添加了我的完整代码。恐怕我现在不能做更多了。
    猜你喜欢
    • 2023-01-05
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 1970-01-01
    • 1970-01-01
    • 2018-11-11
    • 1970-01-01
    • 2020-07-25
    相关资源
    最近更新 更多