<form id =“form1”runat =“server”>的asp.net错误

时间:2015-10-13 13:22:06

标签: javascript c# jquery asp.net

我是新手程序员 我想摆脱传统的javascript警报弹出窗口 我发现弹出一个很好的gui,但它在asp.net中无法正常工作

我的问题是如果我删除

<form id="form1" runat="server">

代码完美无缺

但如果我在asp.net中添加

<form id="form1" runat="server">

弹出窗口不起作用。单击按钮后,它会重新加载整个站点,而不是弹出消息。为什么? 这是整个代码

<script type="text/javascript">
            /**
             * See how to limit only one dialog can be opened at a time.
             */
            $(function () {
                var $btn = $('#btn-open');
                $btn.on('click', function (event) {
                    BootstrapDialog.closeAll();
                    var dialog = new BootstrapDialog({
                        message: 'The only one.'
                    });
                    dialog.open();
                });
            });
        </script>
        <style type="text/css">
            .floating-menu {
                background-color: gray;
                border: 1px solid blue;
                padding: 20px;
                position: absolute;
                top: 10px;
                right: 10px;
                z-index: 9999;
            }
        </style>
<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    
    <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script>
        <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/prettify/r298/run_prettify.min.js"></script>
        <link href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.5/css/bootstrap-dialog.min.css" rel="stylesheet" type="text/css" />
        <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap3-dialog/1.34.5/js/bootstrap-dialog.min.js"></script>
        <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css"/>
        <title>Only one dialog</title>
        

</head>
<body>
    <form id="form1" runat="server">
    <div>
    <div class="floating-menu">
            <button class="btn btn-primary" id="btn-open">Open dialog</button>
        </div>
        
    </div>
    </form>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

因为默认情况下button控件是submit类型并且它会将表单发布到服务器,为了避免这种情况,您可以将其指定为按钮,如下所示: -

<button type="button" class="btn btn-primary" id="btn-open">Open dialog</button>

否则你可以使用jQuery preventDefault方法: -

 $(function () {
     var $btn = $('#btn-open');
     $btn.on('click', function (event) {
         event.preventDefault();
         BootstrapDialog.closeAll();
         var dialog = new BootstrapDialog({
              message: 'The only one.'
         });
         dialog.open();
         });
  });