用VS2012设计一个管理员登录页面,要求使用验证控件,密码不能为空,当验证失败和通过验证时显示,密码为123,写出代码
时间: 2024-01-06 13:07:00 浏览: 35
好的,以下是使用ASP.NET的C#语言编写的管理员登录页面代码,使用了验证控件和密码非空验证,密码为123:
```html
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="AdminLogin.aspx.cs" Inherits="WebApplication.AdminLogin" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>管理员登录</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>管理员登录</h2>
<asp:Label ID="lblMsg" runat="server" Visible="false" ForeColor="Red"></asp:Label>
<p>
<asp:Label ID="lblUserName" runat="server" Text="用户名:"></asp:Label>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</p>
<p>
<asp:Label ID="lblPassword" runat="server" Text="密码:"></asp:Label>
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvPassword" runat="server" ErrorMessage="密码不能为空" ControlToValidate="txtPassword" ValidationGroup="vgLogin"></asp:RequiredFieldValidator>
</p>
<p>
<asp:Button ID="btnLogin" runat="server" Text="登录" OnClick="btnLogin_Click" ValidationGroup="vgLogin" />
</p>
</div>
</form>
</body>
</html>
```
在代码后端,我们需要编写按钮点击事件 `btnLogin_Click`,在该事件中进行密码验证:
```csharp
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication
{
public partial class AdminLogin : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnLogin_Click(object sender, EventArgs e)
{
if (txtPassword.Text == "123")
{
lblMsg.Visible = true;
lblMsg.ForeColor = System.Drawing.Color.Green;
lblMsg.Text = "登录成功!";
}
else
{
lblMsg.Visible = true;
lblMsg.Text = "密码错误,请重新输入!";
}
}
}
}
```
当密码输入正确时,会显示“登录成功”,否则会显示“密码错误,请重新输入”。同时,由于使用了验证控件,当密码为空时会提示“密码不能为空”。