按钮不会触发方法

时间:2016-08-04 18:02:18

标签: c# asp.net

标题真的说明了一切。我按下按钮,没有任何反应试图找出原因。

Default.aspx标记(在表单范围内)

 <div class="form-group">
     <div style="text-align:center">
        <asp:Button input type="submit" class="btn btn-default btn-lg" align="center"  runat="server" OnClick="btnSubmit_Click" Text="Submit"/>
     </div>
 </div>

Default.aspx代码

 protected void btnSubmit_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection();

        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('Connected to ASPX file');", true); 
        //...rest of code
    }

已添加表格

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <%-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags --%>
    <meta name="description" content="">
    <meta name="author" content="">

    <%--- Dependencies ---%>
    <link rel="stylesheet" href="./Content/bootstrap.min.css"/>
    <link rel="stylesheet" href="./Content/bootstrap-theme.min.css"/>
    <!-- IE10 viewport hack for Surface/desktop Windows 8 bug -->
    <link href="./Content/ie10-viewport-bug-workaround.css" rel="stylesheet"/>
    <link href="./Content/starter-template.css" rel="stylesheet"/>
    <script src="./Scripts/ie-emulation-modes-warning.js"></script>

     <title>CCPOF</title>
</head>

  <body>
    <form class="form-horizontal" id="form1" runat="server">
    <div>


        <div class="form-group">
            <div style="text-align:center">
                <asp:Button class="btn btn-default btn-lg" align="center" runat="server" OnClick="btnSubmit_Click" Text="Submit"/> 
                <!--<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" CssClass="btn btn-default btn-lg" />
                    <asp:Button input type="submit" class="btn btn-default btn-lg" align="center"  runat="server" OnClick="btnSubmit_Click" Text="Submit"/> -->
            </div>
        </div>

        <br>

    </div>
    </form>

    <!-- Bootstrap core  JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->

    <script src="./Scripts/jquery-1.9.1.min.js"></script>
    <script>window.jQuery || document.write('<script src="../../assets/js/vendor/jquery-1.9.1.min.js"><\/script>')</script>
    <script src="./Scripts/bootstrap.min.js"></script>
    <script src="./Scripts/ie10-viewport-bug-workaround.js"></script>
    <embed id="embed_npfido" type="application/npfido" height="0"/>

</body>
</html>

1 个答案:

答案 0 :(得分:0)

刚刚结束这个问题,我正在通过与OP的沟通写出答案(请参阅评论)

对OP进行了一些改变以获得回发功能

将按钮HTML更改为

<asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" CssClass="btn btn-default btn-lg" />

将警报脚本更改为

ClientScript.RegisterClientScriptBlock(this.GetType(), "myalert", "<script>alert('Connected to ASPX file');</script>");

编辑:

我认为OP没有得到提醒,因为他错过了<script>标签