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中打开,输入正确用户名密码,点击提交

相关推荐
江湖人称小鱼哥6 小时前
Prisma 命令安全指南
数据库·安全·prisma
ahauedu6 小时前
Spring Boot 2.7+ 中 RedisConnectionFactory Autowire 警告的深度解析
java·spring boot·后端
Gauss松鼠会6 小时前
【openGauss】1分钟掌握:openGauss活动会话CPU占用率获取
数据库·database·opengauss
豆沙沙包?7 小时前
2025年--Lc182--sql(排序和分组)--Java版
java·数据库·sql
IT_陈寒7 小时前
React 性能优化:5个实战技巧让首屏加载提升50%,开发者亲测有效!
前端·人工智能·后端
CryptoRzz7 小时前
欧美(美股、加拿大股票、墨西哥股票)股票数据接口文档
java·服务器·开发语言·数据库·区块链
大厂码农老A8 小时前
你打的日志,正在拖垮你的系统:从P4小白到P7专家都是怎么打日志的?
java·前端·后端
摇滚侠8 小时前
Spring Boot 3零基础教程,深度理解 Spring Boot 自动配置原理,笔记11
spring boot·笔记·后端
APItesterCris8 小时前
构建弹性数据管道:利用淘宝商品 API 进行流式数据采集与处理
linux·数据库·windows
九河云8 小时前
TOS + 数字孪生:集装箱码头的智能进化密码
大数据·服务器·网络·数据库·数字化转型