Click here to Skip to main content
15,887,415 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
server side method is called only if textbox is empty. This is login form. When I type any username and password, the server side method is not a called at all. If I leave the textboxes empty, everything works . Please help

ASP.NET
<form  class="login-form" runat="server">
             <asp:ToolkitScriptManager ID="ToolkitScriptManager1" EnablePageMethods="true" runat="server"></asp:ToolkitScriptManager>

                                        <div class="form-group">

                                            <label class="sr-only" for="form-username">Username</label>
                                            <input  id="Username" type="text" placeholder="Username..." class="form-username form-control" />
                                        </div>
                                        <div class="form-group">
                                            <label class="sr-only" for="form-password">Password</label>
                                            <input type="password" id="Password" placeholder="Password..." class="form-password form-control" />
                                        </div>


               <button  class="btn" onclick="CheckLogin()" >Sign in!</button>
                                    <label id="message"></label>
</form>


Below is the javascript code. It returns data that is used for further checks. If the data is true, we store a session and redirect to index page.

HTML
<script>
            function CheckLogin() {
                var username = $('#Username').val();
                var password = $('#Password').val();
                alert(password);
                    PageMethods.LoadDetails(username, password, function (data) {
                    if (data == "TRUE") {
                        Session("Userid") = username;
                        window.location.href = "index.aspx";
                    }
                    else {
                        $('#message').html(data);
                    }

                });
            }

    </script>


below is the vb code. It calls a class called Marks which contains a function called Load_Details. This function checks to see if user exist and returns true if he does exist.
VB
Imports System.Web.Services

        <System.Web.Services.WebMethod()> _
        Public Shared Function LoadDetails(ByVal Username As String, ByVal Password As String) As String
            Dim Mkz As Marks = New Marks()
            Dim holmes As String
            holmes = Mkz.load_Details(Username, Password)
            Return holmes
        End Function
Posted
Updated 7-Jul-15 20:35pm
v2

1 solution

So you use javascript to call a server method then re-direct the user if the login is successful? That's what a form does naturally, just use a form. As for your code, there are a few possible issues. Mainly you're not returning true or false from your CheckLogin method and not using "return" in the "onlcick" attribute on your button, so your form is submitted regardless. Your "Session" code isn't going to work as that is server-code, you can't use it in javascript, your webmethod is going to need to set the session value. There might be other things, learn to debug your code using the browser tools and VisualStudio tools.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900