Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling Server side event from html button control Not Working

Tags:

html

c#

asp.net

I create a simple log-in page which used .js and .css. Originally, it's an HTML web page. So, I put it on my ASP.net and modified it. However, It seems like my LOGIN buttons are not working. Below here is my code :

Already added "runat="server" onserverclick="btnLogin_Click" and tried but somehow button not working without errors.

<%@ Page Language="C#" AutoEventWireup="True" CodeBehind="Login.aspx.cs" Inherits="LogIn.Login" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Login V2</title>
    <meta charset="UTF-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>

    <!-- Favicon -->
    <link rel="icon" type="image/png" href="images/icons/favicon.ico"/>  

    <!-- Bootstrap -->
    <link rel="stylesheet" type="text/css" href="vendor/bootstrap/css/bootstrap.min.css"/>

    <!-- Font-Awesome -->
    <link rel="stylesheet" type="text/css" href="fonts/font-awesome-4.7.0/css/font-awesome.min.css"/>

    <!-- Material Design -->
    <link rel="stylesheet" type="text/css" href="fonts/iconic/css/material-design-iconic-font.min.css"/>

    <!-- Animate -->
    <link rel="stylesheet" type="text/css" href="vendor/animate/animate.css"/>

    <!-- Hamburgers -->
    <link rel="stylesheet" type="text/css" href="vendor/css-hamburgers/hamburgers.min.css"/>

    <!-- Animsition -->
    <link rel="stylesheet" type="text/css" href="vendor/animsition/css/animsition.min.css"/>

    <!-- Select2 -->
    <link rel="stylesheet" type="text/css" href="vendor/select2/select2.min.css"/>

    <!-- Daterangepicker -->
    <link rel="stylesheet" type="text/css" href="vendor/daterangepicker/daterangepicker.css"/>

    <!-- Custom css -->
    <link rel="stylesheet" type="text/css" href="css/util.css"/>
    <link rel="stylesheet" type="text/css" href="css/main.css"/>

</head>
<body>

<div class="limiter">
        <div class="container-login100">
            <div class="wrap-login100">
                <form class="login100-form validate-form">
                    <span class="login100-form-title p-b-26">
                        Welcome
                    </span>
                    <span class="login100-form-title p-b-48">
                        <i class="zmdi zmdi-font"></i>
                    </span>

                    <div class="wrap-input100 validate-input" data-validate = "Valid email is: [email protected]">
                        <input class="input100" type="text" name="txtUserName" id="txtUserName" runat="server"/>
                        <span class="focus-input100" data-placeholder="Email"></span>
                    </div>

                    <div class="wrap-input100 validate-input" data-validate="Enter password">
                        <span class="btn-show-pass">
                            <i class="zmdi zmdi-eye"></i>
                        </span>
                        <input class="input100" type="password" name="txtPassword" id="txtPassword" runat="server"/>
                        <span class="focus-input100" data-placeholder="Password"></span>
                    </div>

                    <div class="container-login100-form-btn">
                        <div class="wrap-login100-form-btn">
                            <div class="login100-form-bgbtn"></div>
                            <button class="login100-form-btn" id="btnLogin" runat="server" onserverclick="btnLogin_Click">
                                Login
                            </button>                            
                        </div>
                    </div>

                    <div class="text-center p-t-115">
                        <span class="txt1">
                            Don’t have an account?
                        </span>

                        <a class="txt2" href="#">
                            Sign Up
                        </a>
                    </div>
                </form>
            </div>
        </div>
    </div>


    <div id="dropDownSelect1"></div>

    <!-- Jquery -->
    <script src="vendor/jquery/jquery-3.2.1.min.js"></script>

    <!-- Animsition -->
    <script src="vendor/animsition/js/animsition.min.js"></script>

    <!-- Popper and Bootstrap -->
    <script src="vendor/bootstrap/js/popper.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.min.js"></script>

    <!-- Select2 -->
    <script src="vendor/select2/select2.min.js"></script>

    <!-- Moment -->
    <script src="vendor/daterangepicker/moment.min.js"></script>
    <script src="vendor/daterangepicker/daterangepicker.js"></script>

    <!-- Countdowntime -->
    <script src="vendor/countdowntime/countdowntime.js"></script>

    <!-- Custom main.js -->
    <script src="js/main.js"></script>


</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;


namespace LogIn
{
    public partial class Login : System.Web.UI.Page
    {
        //string strqry, User, Password;
        String User, Password;
        String UserID;
        String UserType;
        int RowCount;

        protected void Page_Load(object sender, EventArgs e)
        {
            // lblErrorMessage.Visible = false;

        }       

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //test purpose
            using (SqlConnection sqlCon = new SqlConnection(@"Data Source=mydate source, username, pw     "))
            {

                using (SqlCommand cmd = new SqlCommand("sp_CheckUser", sqlCon))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd.CommandText, sqlCon))
                    {
                        DataTable dt = new DataTable();
                        da.Fill(dt);
                        RowCount = dt.Rows.Count;
                        for (int i = 0; i < RowCount; i++)
                        {

                            User = dt.Rows[i]["UserName"].ToString();
                            Password = dt.Rows[i]["Password"].ToString();
                            UserID = dt.Rows[i]["UserID"].ToString();

                            if (User == txtUserName.Value && Password == txtPassword.Value)
                            {

                                Session["UserName"] = User;
                                Session["UserID"] = UserID;
                                Response.Redirect("Home.aspx");

                            }
                            else
                            {
                                //lblErrorMessage.Visible = true;
                            }
                        }
                    }
                }
            }

        }
    }
}
like image 264
java404 Avatar asked Mar 04 '23 07:03

java404


1 Answers

Sounds like your problem coming from the <form> element, which doesn't have runat="server" attribute (and unique id to identify it as form server control):

<!-- this form is not processed server-side -->
<form class="login100-form validate-form">
    <!-- HTML elements -->
</form>

Note that any server controls from plain HTML elements requires a form tag with runat="server" to be able using onserverclick attribute, otherwise they won't work as server control and treated as normal HTML elements.

<form class="login100-form validate-form" id="form1" runat="server">
    <!-- HTML elements -->
</form>
like image 182
Tetsuya Yamamoto Avatar answered Mar 24 '23 11:03

Tetsuya Yamamoto