ASP.NET第五章 --案例

第五章

一、

1.打开Visual Studio

2.新建ASP.NET项目 Demo

3.创建web窗体,名字叫index1

4.打开Global.asax.cs

cs 复制代码
protected void Session_Start(object sender, EventArgs e)

{

         Response.Write("开始一个新的会话!Session_Start <br />");

     }



protected void Application_BeginRequest(object sender,EventArgs e)

     {

         Response.Write("开始执行!Application_BeginRequest <br />");

      }
  1. 打开index1.asax.cs
cs 复制代码
protected void Page_Load(object sender, EventArgs e)

{

    Response.Write("开始执行!Page_Load <br />");

}

6.执行index1

刷新一下

会话就没了,因为现在是第二个了

二、

  1. 打开Global.asax.cs
cs 复制代码
// 应用程序开始时,执行此事件

        protected void Application_Start(object sender, EventArgs e)

        {

            // 清零

            // 1.上锁

            Application.Lock();

            // 2.对我们当前人数进行初始化 0

            Application["UserNum"] = 0;  // 对象级

            // 3.解锁

            Application.UnLock();

        }



        // 会话开始时执行此事件

        protected void Session_Start(object sender, EventArgs e)

        {

            Response.Write("开始一个新的会话!Session_Start <br />");

            // 增加在线人数

            // 1.上锁

            Application.Lock();

            // 2.让当前会话人数+1,并且赋值到一个新的变量中

            // Convert.ToInt32() 强制数据类型转换

            Application["UserNum"] = Convert.ToInt32(Application["UserNum"]) + 1;

            // 3.解锁

            Application.UnLock();

        }



        // 会话结束时,执行此事件

        protected void Session_End(object sender, EventArgs e)

        {

            Response.Write("结束会话!Session_End <br />");

            // 减少在线人数

            // 1.上锁

            Application.Lock();

            // 2.让当前会话人数-1,并且赋值到一个新的变量中

            // Convert.ToInt32() 强制数据类型转换

            Application["UserNum"] = Convert.ToInt32(Application["UserNum"]) - 1;

            // 3.解锁

            Application.UnLock();

        }

2.添加一个新的web窗体,名字叫app

3.在app.asax.cs里面接收一下全局变量

cs 复制代码
// 页面加载事件

        protected void Page_Load(object sender, EventArgs e)

        {

            // 显示当前在线人数

            Response.Write("欢迎登录,您是第" + Application["UserNum"] + "位用户");

        }

4.Ctrl+F5打开看一下

换浏览器再开一下

三、

  1. 添加一个新的web窗体,名字叫index2

2.验证用户名密码首先要先写个简单的登录页面

html 复制代码
<body>

    <form id="form1" runat="server">

        <div>

            您的姓名:<asp:TextBox ID="txtName" runat="server"></asp:TextBox>

            <br />

            您的密码:<asp:TextBox ID="txtPwd" runat="server"></asp:TextBox>

        </div>

        <asp:Button ID="btn" runat="server" Text="提交" />

    </form>

</body>

3.打开设计页面,双击提交按钮

cs 复制代码
// 提交按钮的点击事件

        protected void btn_Click(object sender, EventArgs e)

        {

            // 1.获取控件中姓名和密码的值

            string strName = txtName.Text;

            string strPwd = txtPwd.Text;



            // 2.把用户名存入Session中,密码通过Url的方式传递

            // 3.判断,如果用户名为"张三",并且(与)密码为"123456",那就让他执行第二步

            // 逻辑运算符:"&&与" "||或" "!非"

            if (strName == "张三" && strPwd == "123456")

            {

                // 4.用户名存入Session中

                // Session["UserName"] 键

                Session["UserName"] = strName;

                // 5.密码通过Url的方式传递  带着密码的值跳转到index3.aspx页面

                Response.Redirect("index3.aspx?pwd=" + strPwd);

            }

            else

            {

                Response.Write("输入的用户名或密码不正确!");

            }

        }
  1. 添加一个新的web窗体,名字叫index3

5.Ctrl+F5打开试一下

输入用户名密码正确,点击提交,跳转到index3页面,并在url上显示密码

输入用户名密码不正确,在当前页面提示

6.打开index3.aspx.cs

cs 复制代码
// 页面加载事件

        protected void Page_Load(object sender, EventArgs e)

        {

            // 1.接收index2.aspx页面传输过来的值

            // 2.判断,当前Session的姓名不为空

            if (Session["UserName"] != null)

            {

                // 3.显示用户名和密码

                // QueryString 查找虚拟路径中变量的集合

                Response.Write("欢迎:" + Session["UserName"] + ",您的密码:" + Request.QueryString["pwd"]);

            }

        }

7.在index2.aspx中打开,输入正确用户名密码,点击提交

相关推荐
knoci2 小时前
【Go】-基于Gin框架的IM通信项目
开发语言·后端·学习·golang·gin
傻Q爱4 小时前
.NET 控制台应用程序连接 MySQL 数据库实现增删改查
mysql·c#
高高要努力4 小时前
SpringBoot日志集成-LogBack
spring boot·后端·logback
Pandaconda5 小时前
【计算机网络 - 基础问题】每日 3 题(二十七)
开发语言·经验分享·笔记·后端·计算机网络·面试·职场和发展
Pandaconda5 小时前
【计算机网络 - 基础问题】每日 3 题(二十四)
开发语言·经验分享·笔记·后端·计算机网络·面试·职场和发展
水上冰石6 小时前
springboot+neo4j demo
spring boot·后端·neo4j
啵一杯6 小时前
leetcode621. 任务调度器
服务器·前端·数据结构·算法·c#
月巴月巴白勺合鸟月半6 小时前
电子数据交换EDI 835 的处理
开发语言·c#·健康医疗·医保