onserverclick按钮不会响应

时间:2015-09-30 03:35:38

标签: c# asp.net

我在

中有这段代码

ASPX:

<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button"
                    class="navbar-toggle collapsed"
                    data-toggle="collapse"
                    data-target="#navbar"
                    aria-expanded="false"
                    aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand"
               href="#">Project name</a>
        </div>
        <div id="navbar"
             class="navbar-collapse collapse">
            <form class="navbar-form navbar-right">
                <div class="form-group">
                    <input type="text"
                           placeholder="Username"
                           class="form-control"
                           runat="server" required
                           id="username" />
                </div>
                <div class="form-group">
                    <input type="password"
                           placeholder="Password"
                           class="form-control"
                           runat="server" required
                           id="password" />
                </div>
                <input type="submit"
                       class="btn btn-success"
                       runat="server"
                       onserverclick="login_Click"
                       value="Sign in" />
            </form>
        </div>
        <!--/.navbar-collapse -->
    </div>
</nav>

每当我按下按钮时,它都不会转到Code Behind中的login_Click函数。有人帮助我,谢谢。

2 个答案:

答案 0 :(得分:2)

有两个变化可以解决这个问题

首先是给按钮提供ID,然后添加OnClick这样的东西

<input type="submit"
       class="btn btn-success"
       runat="server"
       onserverclick="login_Click"
       value="Sign in"
       onclick="__doPostBack('login','')"
       id="login" />

答案 1 :(得分:1)

就像@Mohit说首先在 YourPageName.aspx 页面中的输入标记中提供ID

<input type="submit" class="btn btn-success" runat="server" onserverclick="login_ServerClick" id="login" value="Sign in" />

最后你需要在代码中添加一个on click事件 YourPageName.aspx.cs

protected void login_ServerClick(object sender, EventArgs e)
{
    //string a = "alert('your request has been submitted successfully !');";
    //ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "x", a, true);
}