.net之旅-用户登录框架[还不错,转自CSDN]

 

新一篇: .net之旅-用户登录框架2(40)

用户登录模块基本上是系统不可或缺的。本文将有关这方面的内容进行了简介和编码。不包括数据库部分。内容仅包括登录的验证(验证输入是否为空,并提示对应的出错信息)、回车即提交的实现。可以说是比较完整的一个框架。后面的部分我写完继续贴出。

1 建立网站在VS2005中,这个就不用我说了吧。

2 建立Index.aspx文件,系统同时建立Index.aspx.cs文件。

3 Index.aspx的代码:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Index.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >

<script language="javascript">
/* 回车提交表单 */
function SubmitKeyClick(button) 
{    
    
if (event.keyCode == 13
    
{        
        document.getElementById(
"ButtonLogin").click();
    }

}

</script> 

<head runat="server">
    
<title>欢迎光临</title>
</head>
<body>
    
<form id="formLogin" runat="server">
    
<table>
        
<tr>
            
<td style="height: 26px;  67px;">用户名:</td>
            
<td style=" 160px; height: 26px"><asp:TextBox ID="TextBoxUserId" runat="server"></asp:TextBox></td>
        
</tr>
        
<tr>
            
<td style=" 67px">密  码:</td>
            
<td style=" 160px"><asp:TextBox ID="TextBoxUserPwd" runat="server"></asp:TextBox></td>
        
</tr>
        
<tr>
            
<td></td>
            
<td style="height: 26px;  160px;" align="center"><asp:Button ID="ButtonLogin" runat="server" Text="登录" ToolTip="用户登录" OnClick="ButtonLogin_Click" /></td>
        
</tr>
        
<tr>
        
<td></td>
        
<td><asp:Label ID="LabelError" runat="server" Text="用户名或密码错误" Visible="False" ForeColor="Red"></asp:Label></td>
        
</tr>
    
</table>
    
</form>    
</body>
</html>

4 对应的cs文件代码:

/// ************************************************************
/// Copyright (C), 2006-2007, GUET.
/// FileName: Index.aspx.cs
/// Author: longronglin
/// Version : 1.0
/// Date: 2007-01-23
/// Description:      
/// Function List:   
///     1. void Page_Load()
///     2. void ButtonLogin_Click()
///     3. void ValidInput()
/// History:      
///      <author> <time> <version> <desc>
///      longronglin    2007/01/23     1.0      modify xxx . 
/// *************************************************************


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        
/// 回车提交表单处理,调用JavaScript处理。
        
/// onkeydown为TextBoxUserPwd的事件
        
/// SubmitKeyClick为对应的JavaScript函数,ButtonLogin为对应按钮ID的标识

        TextBoxUserPwd.Attributes.Add("onkeydown""SubmitKeyClick('ButtonLogin');");

        
///设置为焦点
        TextBoxUserId.Focus();
    }


    
/// <summary>
    
/// 登录按钮处理
    
/// </summary>
    
/// <param name="sender"></param>
    
/// <param name="e"></param>

    protected void ButtonLogin_Click(object sender, EventArgs e)
    
{
        ValidInput();

    }


    
/// <summary>
    
/// 验证输入是否合法,输入的用户名和密码不可为空。
    
/// </summary>

    private void ValidInput()
    
{
        
/// Page.IsPostBack响应客户端的反应判定,客户端重新提交则为真。
        if (Page.IsPostBack)
        
{
            LabelError.Visible 
= false;
            
if (TextBoxUserId.Text.Length == 0
            
{
                LabelError.Text 
= "用户名不可为空";
                LabelError.Visible 
= true;
                
                
///设置为焦点
                TextBoxUserId.Focus();
                
/// return用来退出当前函数
                return;
            }

            
if (TextBoxUserPwd.Text.Length == 0)
            
{
                LabelError.Text 
= "密码不可为空";
                LabelError.Visible 
= true;

                
///设置为焦点
                TextBoxUserPwd.Focus();
            }

        }

    }


}

5 测试正确。

原文地址:https://www.cnblogs.com/angelfeather/p/1224765.html