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

本文介绍了一个简单的.NET用户登录框架,包括验证输入是否为空并显示错误信息的功能,以及如何实现回车即可提交表单。该框架不含数据库部分。
 

新一篇: .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; width: 67px;" > 用户名: </ td >
            
< td  style ="width: 160px; height: 26px" >< asp:TextBox  ID ="TextBoxUserId"  runat ="server" ></ asp:TextBox ></ td >
        
</ tr >
        
< tr >
            
< td  style ="width: 67px" > 密  码: </ td >
            
< td  style ="width: 160px" >< asp:TextBox  ID ="TextBoxUserPwd"  runat ="server" ></ asp:TextBox ></ td >
        
</ tr >
        
< tr >
            
< td ></ td >
            
< td  style ="height: 26px; width: 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/archive/2008/06/18/1224765.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值