从html按钮控件调用服务器端事件不工作

问题描述 投票:4回答:2

我创建了一个使用.js和.css的简单登录页面。最初,它是一个HTML网页。所以,我把它放在我的ASP.net上并修改它。但是,似乎我的LOGIN按钮不起作用。下面是我的代码:

已添加“runat =”server“onserverclick =”btnLogin_Click“并尝试但不知何故按钮无法正常工作而没有错误。

<%@ 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;
                            }
                        }
                    }
                }
            }

        }
    }
}
c# asp.net html5
2个回答
5
投票

听起来像你的问题来自<form>元素,它没有runat="server"属性(和唯一的id将其识别为表单服务器控件):

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

请注意,纯HTML元素的任何服务器控件都需要带有runat="server"的表单标记才能使用onserverclick属性,否则它们将无法用作服务器控件并被视为普通HTML元素。

<form class="login100-form validate-form" id="form1" runat="server">
    <!-- HTML elements -->
</form>

2
投票

您需要在表单标记中添加runat =“server”属性

        <form runat="server" class="login100-form validate-form">
© www.soinside.com 2019 - 2024. All rights reserved.