ASP.net(按钮)

ASP.net (Button)

我想隐藏div,但是当我点击隐藏div时它会再次显示。

<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
    <asp:Button runat="server" ID="Name" CssClass="btn" Text="Add Producd" OnClientClick="toggle()" />
    <script type="text/javascript">
    function toggle() {
        var x = document.getElementById('Workshop');
        if (x.style.display === 'none') {
            x.style.display = 'block'
        }
        else {
            x.style.display = 'none';
        }
    }
    </script>
    <div id="Workshop">
        <h1 style="background-color: red">Test</h1>
    </div>
</ContentTemplate>
</asp:UpdatePanel>

您的 asp:Button 导致回发,导致页面重新加载,div 处于默认显示状态。您可以在 OnClientClick 中禁用回发:

<asp:Button runat="server" ID="Name" CssClass="btn" Text="Add Producd" OnClientClick="toggle(); return false;" />

或者您可以使用不同的控件:

<input type="button" name="Name" id="Name" onclick="toggle()" class="btn" value="Add Producd" />