gpt4 book ai didi

c# - 从 html 按钮控件调用服务器端事件不起作用

转载 作者:太空狗 更新时间:2023-10-30 00:37:14 26 4
gpt4 key购买 nike

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

已添加“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: a@b.c">
<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;
}
}
}
}
}

}
}
}

最佳答案

听起来您的问题来自 <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>

关于c# - 从 html 按钮控件调用服务器端事件不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55332490/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com