ASP.NET OnServerClick and OnClientClick

I am having trouble with this:

Here is my login.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="EcnManager.Web.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <link href="Content/bootstrap.min.css" rel="stylesheet" />        
        <script src="Scripts/jquery-1.9.1.js"></script>        
    </head>
    <body>
        <div class="container">
            <div class="row" style="height:30px">
                <div class="col-lg-12"></div>
            </div>
            <div class="row">
                <div class="col-lg-4"></div>
                <div class="col-lg-4">
                    <div class="panel panel-primary">
                        <div class="panel-heading">ECN Manager Login</div>
                        <div class="panel-body">
                            <form role="form" runat="server">                                
                                <div class="form-group">
                                    <label for="txtUserName" class="control-label">Login Name</label>
                                    <input id="txtUserName" runat="server" class="form-control" placeholder="Login Name"/>
                                <%--<asp:TextBox ID="txtUserName" runat="server" CssClass="form-control"></asp:TextBox>--%>
                                </div>
                                <div class="form-group has-error">
                                    <label for="txtPassword" class="control-label">Password</label>
                                    <input id="txtPassword" runat="server" type="password" class="form-control" placeholder="Password"/>
                                <%--<asp:TextBox ID="txtPassword" runat="server" TextMode="Password" CssClass="form-control" ></asp:TextBox>--%>
                                </div>
                                <div class="form-group">
                                    <div class="row">
                                        <div class="col-lg-3">
                                            <%--<input runat="server" id="btnLogin" type="button" value="Login" class="btn btn-default" onserverclick="btnLogin_Click"/>--%>
                                            <asp:Button ID="btnLogin" runat="server" class="btn btn-default" Text="Login" OnClientClick="btnLoginClick()" OnClick="btnLogin_Click" />
                                        </div>
                                        <div class="col-lg-9">
                                            <asp:CheckBox ID="chkRemeberMe" runat="server" />
                                            <label class="text-center">Remember Me</label>
                                        </div>
                                    </div>
                                </div>
                            </form>
                        </div>
                        <div class="panel-footer"></div>
                    </div>
                </div>
                <div class="col-lg-4"></div>
            </div>
            <div class="row" style="height:30px">
                <div class="col-lg-12"></div>
            </div>
        </div>
         <script type="text/javascript">
             function btnLoginClick() {
                 alert("im here");
                 return false;
             };
        </script>      
    </body>
</html>

When I start and click btnLogin, I get "im here", but it also goes and fires the event on the server side. I pass false to btnLoginClick to prevent this from happening. I'm missing something. Any help is appreciated.

EDIT

The answer of Yuri Galanter did this. Thanks

Now I'm trying to answer jcalabris so that I can find out

so I did the following without using the asp button, but inputting a button like

<input runat="server" id="btnLogin" type="button" value="Login" class="btn btn-default" onserverclick="btnLogin_Click"/>
<%--<asp:Button ID="btnLogin" runat="server" class="btn btn-default" Text="Login" OnClientClick="btnLoginClick()" OnClick="btnLogin_Click" />--%>

I changed my javascript code as follows

<script type="text/javascript">
    $("#btnLogin").click(function(){
        event.preventDefault();
        return false;
    });
</script>  

That's right, it didn’t work for me

I also tried the following

<script type="text/javascript">
    $("#btnLogin").click(function(e){
        e.preventDefault();
        return false;
    });
</script>  

I think I'm doing something wrong.

Appreciate your help a lot.

thank

+3
source share
1 answer

false .

OnClientClick="btnLoginClick()"

:

OnClientClick="return btnLoginClick()"

function btnLoginClick false - , ,

+2

All Articles