Page_Load事件

本文介绍了一个简单的ASP.NET登录页面实现方法,通过Page_Load事件和IsPostBack属性来判断页面是否首次加载,并根据用户输入验证登录信息。

Page_Load事件---IsPostBack属性 

aspx代码

 

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.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" >
<head runat="server">
    
<title>无标题页</title>
</head>
<body>
    
<form id="form1" runat="server">
    
<div style="text-align: left">
        
<br />
        
<br />
        
<br />
        
<br />
        
&nbsp; &nbsp;&nbsp;
        
<asp:Label ID="Label1" runat="server" Height="31px" Text="用户名:" Width="95px"></asp:Label>
        
<asp:TextBox ID="textUserName" runat="server" Height="24px" OnTextChanged="textUserName_TextChanged">textUserName</asp:TextBox><br />
        
&nbsp; &nbsp; &nbsp;<asp:Label ID="Label2" runat="server" Height="29px" Text="密码:"
            Width
="93px"></asp:Label>
        
<asp:TextBox ID="textUserPwd" runat="server" Height="22px">textUserPwd</asp:TextBox>
        
<br />
        
<br />
        
&nbsp; &nbsp;&nbsp;
        
<asp:Button ID="btnLogin" runat="server" OnClick="btnLogin_Click" Text="登录" />
        
&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
        
<asp:Button ID="btnClear" runat="server" OnClick="btnClear_Click" Text="清空" />
        
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        
<asp:Button ID="btnClose" runat="server" Text="关闭" /><br />
        
<br />
        
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        
<asp:Label ID="lblMessage" runat="server" Height="66px" Text=" " Width="175px"></asp:Label></div>
    
</form>
</body>
</html>

aspx.cs代码

 

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)
    
{
        
if (!Page.IsPostBack) //判断页面是否是第一次加载
        {
            
this.textUserName.Text= " ";  //将用户名框设置为空
            this.textUserPwd.Text= " ";  //将密码框设置为空
        }

        
this.btnClose.Attributes.Add("onclick""window.close();");//关闭按纽设置,相当于在aspx文件的asp:label中添加onclick="window.close();"
    }


    
protected void btnLogin_Click(object sender, EventArgs e)
    
{
        
if (this.textUserName.Text == "zuo" && this.textUserPwd.Text == "zuo")
        
{
            
this.lblMessage.Text = "登录成功";
        }

        
else
        
{
            
this.lblMessage.Text = "登陆失败";
        }

    }

    
protected void btnClear_Click(object sender, EventArgs e)
    
{
        
this.textUserName.Text = " ";  //设置为空
        this.textUserPwd.Text = " ";  //设置为空
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值